Mac 上的 Node.js Heroku 部署 - sh: 1: nodemon: not found / npm ERR!`nodemon fileName.js` / npm ERR!在...启动脚本失败

Q.M*_*ley 10 macos heroku node.js npm nodemon

在部署HerokuNode.js使用Mac

我的问题:

State changed from starting to crashed &&  
sh: 1: nodemon: not found &&  
Failed at...start script &&
status 1...code=H10
Run Code Online (Sandbox Code Playgroud)

After creating my front-end, with React, back-end server, with node.js/express.js, and database, with PostgreSQL, I attempted to deploy my server on Heroku with Git. Since I already had Git, I moved onto Heroku CLI

First, from the terminal in my server...

brew install heroku/brew/heroku
heroku create
git remote -v
git push heroku master
Run Code Online (Sandbox Code Playgroud)

If this is not your first time using Heroku...

heroku git:remote -a theUrlYouWant
git push heroku master
Run Code Online (Sandbox Code Playgroud)

...otherwise...Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env:

app.listen(process.env.PORT || 3000, () => {  
  console.log(`app is running on port ${process.env.PORT}`);
})  
Run Code Online (Sandbox Code Playgroud)

...if you added port:

git add .
git commit -m "adding port"
git push heroku master
Run Code Online (Sandbox Code Playgroud)

...finally, from my terminal in server:

? folderName git:(master) heroku open  
? folderName git:(master) heroku logs --tail

2019-05-08T18:07:23.253827+00:00 heroku[web.1]: Starting process with command npm start  
2019-05-08T18:07:25.323748+00:00 heroku[web.1]: State changed from starting to crashed  
2019-05-08T18:05:17.074233+00:00 app[web.1]: > nodemon fileName.js  
2019-05-08T18:05:17.074235+00:00 app[web.1]:   
2019-05-08T18:05:17.098124+00:00 app[web.1]: sh: 1: nodemon: not found  
2019-05-08T18:05:17.102512+00:00 app[web.1]: npm ERR! file sh  
2019-05-08T18:05:17.102801+00:00 app[web.1]: npm ERR! code ELIFECYCLE  
2019-05-08T18:05:17.103068+00:00 app[web.1]: npm ERR! errno ENOENT  
2019-05-08T18:05:17.103239+00:00 app[web.1]: npm ERR! syscall spawn    
2019-05-08T18:05:17.104259+00:00 app[web.1]: npm ERR! app@1.0.0 start: nodemon fileName.js  
2019-05-08T18:05:17.104361+00:00 app[web.1]: npm ERR! spawn ENOENT  
2019-05-08T18:05:17.104553+00:00 app[web.1]: npm ERR!  
2019-05-08T18:05:17.104692+00:00 app[web.1]: npm ERR! Failed at the app@1.0.0 start script.  
2019-05-08T18:05:17.104841+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 

[...]

2019-05-08T18:05:17.171915+00:00 heroku[web.1]: Process exited with status 1  
2019-05-08T18:05:37.338695+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=yourURL.herokuapp.com request_id=hidden fwd="ip" dyno= connect= service= status=503 bytes= protocol=https  
Run Code Online (Sandbox Code Playgroud)

Q.M*_*ley 16

Heroku 默认在生产环境中运行,因此它不会安装开发依赖项。

如果您不想将 nodemon 作为依赖项重新安装,我认为您不应该这样做,因为它的正确位置是在 devDependencies 中,而不是在依赖项中...

相反,您可以在您的 中创建第二个 npm 脚本,package.json通过nodemon仅在您的本地主机中运行来避免此错误:

"scripts": {
    "start": "node fileName.js",
    "start:dev": "nodemon fileName.js"
},  
Run Code Online (Sandbox Code Playgroud)

而当你想在本地运行项目,只需要运行在你的终端npm start:dev,它会载入fileName.jsnodemon

在 Heroku 中,npm start默认运行并从普通节点命令加载 fileName.js,然后您就可以摆脱该错误。

2019-05-08T18:13:40.319989+00:00 heroku[web.1]: State changed from crashed to starting  
2019-05-08T18:13:41.000000+00:00 app[api]: Build succeeded  
2019-05-08T18:13:42.658048+00:00 heroku[web.1]: Starting process with command npm start  
2019-05-08T18:13:44.644005+00:00 app[web.1]: 
2019-05-08T18:13:44.644025+00:00 app[web.1]: > app@1.0.0 start /app  
2019-05-08T18:13:44.644027+00:00 app[web.1]: > node fileName.js  
2019-05-08T18:13:44.644028+00:00 app[web.1]:   
2019-05-08T18:13:45.158694+00:00 app[web.1]: app is running on port 33333  
2019-05-08T18:13:46.293205+00:00 heroku[web.1]: State changed from starting to up  
2019-05-08T18:13:47.788861+00:00 heroku[router]: at=info method=GET path="/" host=yourURL.herokuapp.com request_id=hidden fwd="ip" dyno=web.1 connect=0ms service=11ms status=200 bytes=245 protocol=https
Run Code Online (Sandbox Code Playgroud)

我写这篇文章是为了帮助你避免我花时间调试这个问题。

  • 完美的!现在,一旦(我认为是 10 小时?)时间段过后,您可以接受您的答案,请记住这样做。 (2认同)