【问题标题】:React app working well locally but fails after deployment on HerokuReact 应用程序在本地运行良好,但在 Heroku 上部署后失败
【发布时间】:2020-08-03 11:40:59
【问题描述】:

我正在 Heroku 上部署我的 react 网站。它在本地运行良好,并且在运行命令“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 logs -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】:

    您正在尝试在 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
    

    【讨论】:

    • 我的 package.json 文件如下所示:"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject", "build-package": "npm run build-package-css && babel src --out-dir dist" }
    • 这是什么 Heroku 应用程序?是静态的吗?节点?还有什么?
    • 好的,您如何为应用提供服务?脚本名称是什么?也许创建一个Procfile?我用 Procfile 更新了答案
    • 运行 npm start Error: Cannot find module ‘{fileName}/server.js' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15) at Function.Module._load (internal/modules/cjs/loader.js:864:27) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 { code: 'MODULE_NOT_FOUND', requireStack: [] } npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! {fileName}@1.8.0 start: node server.js` npm ERR 后出现此错误!退出状态 1`
    • “server.js”是否存在?我举个例子。你是如何在本地运行你的服务器的?你有快递之类的服务器吗?你确定这不仅仅是一个静态应用程序吗?我需要更多信息才能有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 2021-05-17
    • 2019-11-03
    • 1970-01-01
    • 2020-11-14
    • 2020-08-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多