首页 > 解决方案 > heroku 本地网络工作正常,但部署无法正常工作

问题描述

应用由 Node.js 和 React 组成。没有数据库。Heroku 本地网络工作正常,但部署无法正常工作。部署只有 index.html。简而言之,node 和 react 在部署时不能正常工作。

后端包.json

"engines": {
  "node": "15.4.0",
  "npm": "7.20.5"
},
"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node app.js",
  "heroku-postbuild": "cd frontend && npm install && npm run build"
}

前端包.json

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},
"proxy": "http://localhost:5000"

应用程序.js

app.use(express.static(path.join(__dirname, './frontend/build')));
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname+'/frontend/build/index.html'));
});
const PORT = process.env.PORT || 5000;

档案

web: node app.js

heroku local web上面的代码一切都很好。但是在部署时我遇到了这样的错误:

我得到的错误

Stopping all processes with SIGTERM
heroku[web.1]: Process exited with status 143

我在这里尝试了一些解决方案。但我无法解决。 我可以根据需要写更多信息。谢谢!

标签: javascriptnode.jsreactjsherokudeployment

解决方案


将这些添加到您的前端 package.json 脚本中:

"devStart": "react-scripts start",
"start": "serve -s build",
....
"heroku-postbuild": "npm run build"

推荐阅读