如何为成功部署的React App修复Heroku应用程序错误

时间:2020-04-04 14:52:01

标签: reactjs heroku webpack

美好的一天。我已经成功地将我的React应用程序部署到了Heroku。但是,当我尝试打开时,我的heroku日志中出现以下错误:

2020-04-04T11:31:03.263548+00:00 app[web.1]: ℹ 「wds」: Project is running at http://172.17.176.146/
2020-04-04T11:31:03.263914+00:00 app[web.1]: ℹ 「wds」: webpack output is served from
2020-04-04T11:31:03.264020+00:00 app[web.1]: ℹ 「wds」: Content not from webpack is served from /app/public
2020-04-04T11:31:03.264096+00:00 app[web.1]: ℹ 「wds」: 404s will fallback to /
2020-04-04T11:31:03.264274+00:00 app[web.1]: Starting the development server...
2020-04-04T11:31:03.264276+00:00 app[web.1]: 
2020-04-04T11:31:03.360569+00:00 heroku[web.1]: State changed from starting to crashed
2020-04-04T14:25:45.432183+00:00 heroku[web.1]: State changed from crashed to starting
2020-04-04T14:25:57.190700+00:00 app[web.1]: 
2020-04-04T14:25:57.190727+00:00 app[web.1]: > portfolio@0.1.0 start /app
2020-04-04T14:25:57.190728+00:00 app[web.1]: > react-scripts start
2020-04-04T14:25:57.190728+00:00 app[web.1]: 
2020-04-04T14:25:59.253101+00:00 app[web.1]: ℹ 「wds」: Project is running at http://172.16.211.122/
2020-04-04T14:25:59.253414+00:00 app[web.1]: ℹ 「wds」: webpack output is served from
2020-04-04T14:25:59.253514+00:00 app[web.1]: ℹ 「wds」: Content not from webpack is served from /app/public
2020-04-04T14:25:59.253580+00:00 app[web.1]: ℹ 「wds」: 404s will fallback to /
2020-04-04T14:25:59.253747+00:00 app[web.1]: Starting the development server...
2020-04-04T14:25:59.253749+00:00 app[web.1]: 
2020-04-04T14:25:59.349915+00:00 heroku[web.1]: State changed from starting to crashed
2020-04-04T14:28:20.178274+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kerron-king-portfolio.herokuapp.com request_id=d8f77821-5bd5-4d55-8b95-8fbd37c13c09 fwd="190.213.65.88" dyno= connect= service= status=503 bytes= protocol=https
2020-04-04T14:28:24.241325+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=kerron-king-portfolio.herokuapp.com request_id=c021725e-5389-452d-9601-a7c8018ab331 fwd="190.213.65.88" dyno= connect= service= status=503 bytes= protocol=https

这是一个React应用程序,它是使用create-react-app命令构建的。我没有进行webpack编辑,我的package.json如下:

{
  "name": "portfolio",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.1.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "^2.5.1"
  }
}

我的公用文件夹如下:

- index.html
- robots.txt

这些文件的内容是: index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Portfolio</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

robots.txt:

# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

我在项目中还使用了4张图像,这些图像位于以下目录中:

作品集/ src / imgs

在解决此问题方面的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:18)

好的,我发现了问题。部署时的默认buildpack是node.js。我需要使用create-react-app buildpack(如下所示)。现在可以正常使用了。

heroku create $APP_NAME --buildpack mars/create-react-app
git push heroku master
heroku open

答案 1 :(得分:2)

如果您的应用程序已经部署,那么您仍可以在heroku设置中安装https://github.com/mars/create-react-app-buildpack(如果仍然无法运行)。尝试使用web: bin/boot添加一个procfile。它对我有用,希望对您有用。

答案 2 :(得分:1)

如果使用create-react-app,则应添加一个名为engines的新对象。在引擎对象内部,您可以指定npm和node的版本。为此,请打开您的终端并输入:

npm -v 按Enter

节点-v 按Enter

{
  "name": "portfolio",
  "version": "0.1.0",
  "private": true,
  "engines": {
    "npm": "5.7.1",// here put your version
    "node": "9.5.0" // here put your version
  },
... the rest of the file
}