部署Node App后如何在Heroku上安装依赖项?

时间:2019-11-04 17:47:17

标签: javascript node.js heroku nodemon

我有一个节点应用程序,可以在本地主机上完美运行。但是将其部署到Heroku后会崩溃。

这是错误日志:

2019-11-07T10:58:20.359983+00:00 heroku[web.1]: Starting process with 
command `npm start`
2019-11-07T10:58:22.882837+00:00 heroku[web.1]: State changed from starting to crashed
2019-11-07T10:58:22.866125+00:00 heroku[web.1]: Process exited with status 1
2019-11-07T10:58:22.768738+00:00 app[web.1]: 
2019-11-07T10:58:22.768765+00:00 app[web.1]: > dpapi@1.0.0 start /app
2019-11-07T10:58:22.768768+00:00 app[web.1]: > nodemon --exec babel-node server.js --plugins css-modules-transform --ignore dist/
2019-11-07T10:58:22.768770+00:00 app[web.1]: 
2019-11-07T10:58:22.783839+00:00 app[web.1]: sh: 1: nodemon: not found
2019-11-07T10:58:22.788270+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-11-07T10:58:22.788822+00:00 app[web.1]: npm ERR! syscall spawn
2019-11-07T10:58:22.789278+00:00 app[web.1]: npm ERR! file sh
2019-11-07T10:58:22.789688+00:00 app[web.1]: npm ERR! errno ENOENT
2019-11-07T10:58:22.791368+00:00 app[web.1]: npm ERR! dpapi@1.0.0 start: `nodemon --exec babel-node server.js --plugins css-modules-transform --ignore dist/`
2019-11-07T10:58:22.791637+00:00 app[web.1]: npm ERR! spawn ENOENT
2019-11-07T10:58:22.791917+00:00 app[web.1]: npm ERR!
2019-11-07T10:58:22.792199+00:00 app[web.1]: npm ERR! Failed at the dpapi@1.0.0 start script.
2019-11-07T10:58:22.792534+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-11-07T10:58:22.802491+00:00 app[web.1]: 
2019-11-07T10:58:22.802775+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-11-07T10:58:22.802918+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-11-07T10_58_22_793Z-debug.log

我不确定原因,但似乎找不到nodemon,也许无法安装所有依赖项。如错误消息中所述;

这是我的Procfile:

web: npm start

这是我的package.json

{


"name": "dpapi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec babel-node server.js --plugins css-modules-transform --ignore dist/",
    "dev": "webpack -wd"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/plugin-transform-runtime": "^7.4.4",
    "babel-eslint": "^10.0.1",
    "babel-preset-es2015": "^6.24.1",
    "eslint": "^5.16.0",
    "eslint-plugin-react": "^7.13.0",
    "nodemon": "^1.18.11",
    "webpack-cli": "^3.3.1"
  },
  "dependencies": {
    "@babel/core": "^7.4.4",
    "@babel/node": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-proposal-export-default-from": "^7.2.0",
    "@babel/preset-env": "^7.4.4",
    "@babel/preset-react": "^7.0.0",
    "@material-ui/core": "^4.0.0-rc.0",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "babel-loader": "^8.0.5",
    "bcrypt": "^3.0.6",
    "body-parser": "^1.19.0",
    "buffer": "^5.2.1",
    "dropbox": "^4.0.17",
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "global": "^4.4.0",
    "imagemagick": "^0.1.3",
    "isomorphic-fetch": "^2.2.1",
    "js-file-download": "^0.4.7",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.5.7",
    "morgan": "^1.9.1",
    "multer": "^1.4.1",
    "node-fetch": "^2.5.0",
    "nodemailer": "^6.2.1",
    "query-string": "^6.5.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^7.0.3",
    "react-router-dom": "^5.0.0",
    "react-stripe-elements": "^3.0.0",
    "redux": "^4.0.1",
    "redux-logger": "^3.0.6",
    "rimraf": "^2.6.3",
    "socket.io": "^2.2.0",
    "socket.io-client": "^2.2.0",
    "stripe": "^7.4.0",
    "webpack": "^4.30.0"
  }
}

1 个答案:

答案 0 :(得分:1)

Heroku部署失败,因为它无法在您的nodemon中找到dependencies。简单的解决方案可能是将其移到其中。

更准确的答案是,您当前的配置非常适合开发,但不适用于生产。 我建议您首先使用webpack(或当前使用的任何工具)编译代码,部署构建代码,然后将常规node server.js用作npm start脚本。 如果出现故障,Heroku将负责重启应用程序,您不需要nodemon。