node.js - Heroku 构建成功,但应用程序错误

标签 node.js reactjs heroku deployment

我正在尝试部署一个具有 Node 后端和 react 前端的应用程序。我正在使用 heroku,我的代码如下。它说 mongo 有错误,我尝试重新安装我的 Node 模块。我对 Heroku 没有太多经验,所以任何意见都会很棒。提前致谢。 在 server.js 中 -

if (process.env.NODE_ENV === "production") {
  app.use(express.static("client/build"));

  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  });
}

在 package.json 中 -

{
  "author": "Spotify",
  "name": "web-api-auth-examples",
  "description": "Basic examples of the Spotify authorization flows through OAuth 2",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "client": "npm start --prefix client",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },
  "version": "0.0.2",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "cors": "^2.8.4",
    "dateformat": "^3.0.3",
    "express": "~4.16.0",
    "jsonwebtoken": "^8.3.0",
    "multer": "^1.3.1",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "querystring": "~0.2.0",
    "request": "~2.83.0",
    "request-promise": "^4.2.2",
    "validator": "^10.4.0"
  }
}

heroku 日志 -

2018-08-18T03:08:30.415893+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-08-18T03:08:30.415896+00:00 app[web.1]: npm ERR! errno 1
2018-08-18T03:08:30.415899+00:00 app[web.1]: npm ERR! web-api-auth-examples@0.0.2 start: `node server.js                    `
2018-08-18T03:08:30.415909+00:00 app[web.1]: npm ERR! Failed at the web-api-auth-examples@0.0.2 start sc                    ript.
2018-08-18T03:08:30.415905+00:00 app[web.1]: npm ERR!
2018-08-18T03:08:30.423862+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-08-18T03:08:30.423859+00:00 app[web.1]:
2018-08-18T03:08:30.415901+00:00 app[web.1]: npm ERR! Exit status 1
2018-08-18T03:08:30.415913+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is                     likely additional logging output above.
2018-08-18T03:08:30.423864+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-08-18T03_08_30_416Z-debug                    .log
2018-08-18T03:08:30.536987+00:00 heroku[web.1]: State changed from starting to crashed
2018-08-18T03:08:30.539851+00:00 heroku[web.1]: State changed from crashed to starting
2018-08-18T03:08:30.502667+00:00 heroku[web.1]: Process exited with status 1
2018-08-18T03:08:42.074442+00:00 heroku[web.1]: Starting process with command `npm start`
2018-08-18T03:08:45.349152+00:00 app[web.1]:
2018-08-18T03:08:45.349173+00:00 app[web.1]: > node server.js
2018-08-18T03:08:45.349171+00:00 app[web.1]: > web-api-auth-examples@0.0.2 start /app
2018-08-18T03:08:45.349174+00:00 app[web.1]:
2018-08-18T03:08:46.080144+00:00 app[web.1]: module.js:549
2018-08-18T03:08:46.080165+00:00 app[web.1]: throw err;
2018-08-18T03:08:46.080167+00:00 app[web.1]: ^
2018-08-18T03:08:46.080168+00:00 app[web.1]:
2018-08-18T03:08:46.080171+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2018-08-18T03:08:46.080172+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:547:15)
2018-08-18T03:08:46.080174+00:00 app[web.1]: at Function.Module._load (module.js:474:25)

最佳答案

从您的错误和给定的 package.json 中我了解到,您在应用程序中的某个位置使用了 mongoose,但实际上并未安装 mongoose。

安装 mongoose 来解决问题

npm install -s mongoose

关于node.js - Heroku 构建成功,但应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904981/

相关文章:

javascript - 路由不适用于 React router v4

php - 无法使用简单的 Web 应用程序进行部署/前进

javascript - 如何在 Node js 中循环异步发送批量邮件?

node.js - 我应该硬编码对我的node_modules 文件夹的引用吗?

javascript - V8 如何使用隐藏类和内联缓存优化代码

node.js - Action 必须是普通对象。使用自定义中间件进行异步操作。如何解决这个问题

javascript - React 中的 PSD.js 错误 fs.readFileSync 不是函数

node.js - PostgreSQL REST API Node.js CRUD - 字符串查询参数给出错误

ruby-on-rails - heroku 上的顶级域

ruby-on-rails - 无方法错误 : undefined method `[]' for nil:NilClass when pushing to Heroku