gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:06 26 4
gpt4 key购买 nike

使用 Mac 通过 Node.jsHeroku 上部署

我的问题:

State changed from starting to crashed &&  
sh: 1: nodemon: not found &&
Failed at...start script &&
status 1...code=H10

创建前端后,使用 React,后端服务器,使用 node.js/express.js 和数据库,使用 PostgreSQL,我尝试使用 GitHeroku 上部署我的服务器。由于我已经有了 Git,所以我转向了 Heroku CLI

首先,从我服务器中的终端...

brew install heroku/brew/heroku
heroku create
git remote -v
git push heroku master

如果这不是您第一次使用 Heroku...

heroku git:remote -a theUrlYouWant
git push heroku master

...否则...Heroku 会动态为您的应用分配端口,因此您无法将端口设置为固定数字。 Heroku 将端口添加到环境中:

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

...如果您添加了端口:

git add .
git commit -m "adding port"
git push heroku master

...最后,从我在服务器中的终端:

➜ 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

最佳答案

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

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

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

"scripts": {
"start": "node fileName.js",
"start:dev": "nodemon fileName.js"
},

当你想在本地运行项目时,只需在终端中运行 npm start:dev它将加载 fileName.jsnodemon .

在 Heroku 时,npm start默认情况下运行并从普通 Node 命令加载 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

I made this post in hopes to help you avoid the time it took me to debug this issue.

关于Mac 上的 Node.js Heroku 部署 - sh : 1: nodemon: not found/npm ERR! `nodemon fileName.js`/npm 错误!失败于...启动脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56047981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com