React应用程序在本地运行良好,但在Heroku上部署后失败

问题描述 投票:0回答:1

我正在将我的React网站部署在Heroku上。它在本地运行良好,并在运行命令“ heroku local web”。网站已成功部署,但出现应用程序错误。

An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail

在运行heroku日志-tail时,我的日志文件看起来像这样:

2020-04-20T09:47:52.000000+00:00 app[api]: Build succeeded
2020-04-20T09:47:55.522526+00:00 app[web.1]: ℹ 「wds」: Project is running at https://{some-ip-address}
2020-04-20T09:47:55.523069+00:00 app[web.1]: ℹ 「wds」: webpack output is served from /{filename path}
2020-04-20T09:47:55.523174+00:00 app[web.1]: ℹ 「wds」: Content not from webpack is served from /app/public
2020-04-20T09:47:55.523277+00:00 app[web.1]: ℹ 「wds」: 404s will fallback to /{filename path}
2020-04-20T09:47:55.523531+00:00 app[web.1]: Starting the development server...
2020-04-20T09:47:55.523534+00:00 app[web.1]: 
2020-04-20T09:47:55.631808+00:00 heroku[web.1]: State changed from starting to crashed
2020-04-20T09:48:17.332436+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host={hostname}.herokuapp.com request_id={some_request_id} fwd={ip address} dyno= connect= service= status=503 bytes= protocol=https

什么可能导致失败的原因?我该如何解决?

reactjs heroku web-deployment production-environment heroku-cli
1个回答
0
投票

您正在尝试在Heroku上运行开发服务器,如下所示:

Starting the development server...

您的npm start任务是什么?您有npm run build脚本吗?我敢打赌,您的启动脚本会以监视模式启动webpack?

相反,为您的package.json尝试类似的操作:

{
  //...
  "scripts": {
    "build": "webpack --mode production",
    "dev": "webpack --mode development --watch",
    "start": "node server.js"
  }
}

这假设您正在使用节点服务器,您将需要用正在使用的任何服务器或使用start替换Heroku static buildpack命令。

如果Heroku看到build脚本,它将在部署您的应用程序时运行。

选项2

如果未使用“ npm start”启动服务器,请确保已设置Procfile

web: npm run server
© www.soinside.com 2019 - 2024. All rights reserved.