简体   繁体   English

将节点服务器部署到 heroku 时出错

[英]Having errors deploying Node server to heroku

I am deploying my Node Express server to heroku, and I have some error after I do git push heroku master .我正在将我的 Node Express 服务器部署到 heroku,并且在执行git push heroku master之后出现了一些错误。 I am complete beginner to deploying apps to heroku.对于将应用程序部署到 heroku,我完全是初学者。 How do I fix this error?如何修复此错误?

What I have tried:我试过的:

  • I have set process.env.PORT .我已经设置了process.env.PORT
  • Adding a Procfile with web:node index.js in it添加一个带有web:node index.js的 Procfile
  • heroku restart
2021-11-27T02:39:05.410712+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=logo-eshop.herokuapp.com request_id=fb4f6e95-de99-4135-9273-789af704557d fwd="79.66.155.39" dyno= connect= service= status=503 bytes= protocol=https        
2021-11-27T02:39:05.650439+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=logo-eshop.herokuapp.com request_id=6572c3a7-f7c4-4ef6-ae09-cd4fcf0c6f5f fwd="79.66.155.39" dyno= connect= service= status=503 bytes= protocol=https

Also, I have fixed the nodemon in my package.json:另外,我已经修复了我的nodemon中的 nodemon:

"scripts": {
   "start": "node index.js"
}

But I still have the error.但我仍然有错误。

It actually looks like its because nodemon is not found.它实际上看起来像它,因为找不到 nodemon。

Nodemon should not be used in production or deployment, since you dont need/want to auto refresh your server in your production env. Nodemon 不应在生产或部署中使用,因为您不需要/不想在生产环境中自动刷新服务器。

To prevent this, simply add another field in your scripts section of your package.json file, and remove nodemon from "start".为了防止这种情况,只需在 package.json 文件的脚本部分中添加另一个字段,然后从“开始”中删除 nodemon。 It should look similar to this:它应该类似于:

"scripts": {
    "start": "index.js",
    "dev": "nodemon index.js"
  },

Now, when developing in your own machine, instead of calling npm start (or yarn start), call npm run dev (yarn run dev).现在,在自己的机器上开发时,不要调用 npm start(或 yarn start),而是调用 npm run dev(yarn run dev)。

That way, heroku will never try to use nodemon during production and deployment, but you will be able to auto restart your server during development这样,heroku 将永远不会在生产和部署期间尝试使用 nodemon,但您将能够在开发期间自动重启服务器

Heroku will call npm start, and so if a nodemon command is here, it will fail. Heroku 会调用 npm 启动,所以如果一个 nodemon 命令在这里,它会失败。

I found the solution.我找到了解决方案。 For those who may have the same problem with me, I simply add all my env variables in Heroku (Setting > Config Vars).对于那些可能与我有同样问题的人,我只需将我所有的环境变量添加到env (设置 > 配置变量)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM