【问题标题】:Unable to deploy my MERN stack app on heroku无法在 heroku 上部署我的 MERN 堆栈应用程序
【发布时间】:2021-03-03 21:54:44
【问题描述】:

我创建了一个简单的 MERN 堆栈应用程序并尝试在 heroku 上部署该应用程序,但是,应用程序在出现服务器端构建错误后崩溃。我只是在学习 MERN 堆栈和 heroku,所以我无法弄清楚我的错误。这是我的heroku日志:

2020-11-19T18:13:19.981359+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-19T18:13:32.577655+00:00 heroku[web.1]: Starting process with command `npm start`
2020-11-19T18:13:35.931851+00:00 app[web.1]:
2020-11-19T18:13:35.931868+00:00 app[web.1]: > server@1.0.0 start /app
2020-11-19T18:13:35.931868+00:00 app[web.1]: > concurrently "npm run server" "npm run client"
2020-11-19T18:13:35.931869+00:00 app[web.1]: 
2020-11-19T18:13:35.941884+00:00 app[web.1]: sh: 1: concurrently: not found
2020-11-19T18:13:35.951483+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-11-19T18:13:35.951777+00:00 app[web.1]: npm ERR! syscall spawn
2020-11-19T18:13:35.952007+00:00 app[web.1]: npm ERR! file sh
2020-11-19T18:13:35.952258+00:00 app[web.1]: npm ERR! errno ENOENT
2020-11-19T18:13:35.957255+00:00 app[web.1]: npm ERR! server@1.0.0 start: `concurrently "npm run server" "npm run client"`
2020-11-19T18:13:35.957411+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-11-19T18:13:35.957579+00:00 app[web.1]: npm ERR!
2020-11-19T18:13:35.957727+00:00 app[web.1]: npm ERR! Failed at the server@1.0.0 start script.
2020-11-19T18:13:35.957859+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-11-19T18:13:35.968472+00:00 app[web.1]:
2020-11-19T18:13:35.968693+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-11-19T18:13:35.968767+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-11-19T18_13_35_958Z-debug.log
2020-11-19T18:13:36.042215+00:00 heroku[web.1]: Process exited with status 1
2020-11-19T18:13:36.100527+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-19T18:41:07.000000+00:00 app[api]: Build started by user riteshkumarstudy@gmail.com
2020-11-19T18:41:43.985964+00:00 app[api]: Deploy 1581c630 by user riteshkumarstudy@gmail.com
2020-11-19T18:41:43.985964+00:00 app[api]: Release v4 created by user riteshkumarstudy@gmail.com
ll be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2020-11-19T18:42:59.987409+00:00 app[web.1]: Server listening on port 3001
2020-11-19T18:43:00.110366+00:00 app[web.1]: (node:4) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
2020-11-19T18:43:16.984202+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=pacific-scrubland-58982.herokuapp.com request_id=e41c2163-819d-4d95-a87a-48e84acfd06e fwd="223.225.248.157" dyno= connect= service= status=503 bytes= protocol=https
2020-11-19T18:43:55.839112+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-11-19T18:43:55.866993+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-11-19T18:43:56.004273+00:00 heroku[web.1]: Process exited with status 137
2020-11-19T18:43:56.057821+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-19T18:43:56.610979+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pacific-scrubland-58982.herokuapp.com request_id=f941b09d-91ae-4ddb-87c1-96a66d8c8912 fwd="223.225.248.157" dyno= connect= service= status=503 bytes= protocol=https
2020-11-19T18:43:57.136454+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pacific-scrubland-58982.herokuapp.com request_id=be1491ea-8d01-4b37-9902-6e93678ba0b6 fwd="223.225.248.157" dyno= connect= service= status=503 bytes= protocol=https
2020-11-19T18:43:58.163525+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pacific-scrubland-58982.herokuapp.com request_id=c304480a-ca92-4bf0-9ef2-837931f38d55 fwd="223.225.248.157" dyno= connect= service= status=503 bytes= protocol=https
2020-11-19T18:43:58.312956+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pacific-scrubland-58982.herokuapp.com request_id=7334f985-397f-4f3e-b667-99fd19831635 fwd="223.225.248.157" dyno= connect= service= status=503 bytes= protocol=https

这是我的源代码:https://github.com/CSAIDept/Final-Combined-Pages

由于我仍在学习 MERN 和 heroku,如果您能详细解释我做错了什么以及如何更正它,那就太好了。

【问题讨论】:

    标签: node.js reactjs mongodb heroku web-deployment


    【解决方案1】:

    您正在使用 npm 模块同时启动应用程序。 此模块作为开发依赖项安装,不会在生产中运行。尝试将它放在 package.json 文件中的依赖项下,并将其从 devDependencies 中删除

    【讨论】:

      【解决方案2】:

      错误的原因是您将concurrently 仅作为开发依赖项(这是正确的),但是您正在同时使用来启动服务器。

      要解决这个问题,您需要做一些事情。

      1. 将 package.json 中的 start 命令更改为 dev 命令

        "dev": "concurrently \"npm run server\" \"npm run client\"",
        
      2. 在此添加一个启动命令行

        "start": "npm run server",
        
      3. 然后将它添加到你的 server.js 文件中,就在这一行之前app.listen(3001, () => console.log('Server listening on port 3001'));

        if (process.env.NODE_ENV === "production") {
            app.use(express.static("client/build"));
        
            app.get("*", (req, res) => {
                res.sendFile(path.join(__dirname, "client", "build", "index.html"));
            });
        }
        

      这使得您的服务器为在您的任何路由中找不到的任何请求提供静态 index.html 文件。

      【讨论】:

        【解决方案3】:

        从日志信息来看,问题似乎是Heroku环境没有找到concurrently这个命令。发生这种情况是因为您可能已经在本地机器上全局安装了 concurrently 包,但是当它在 Heroku 机器上运行时它不存在

        我的建议是简单地替换该行

        "start": "concurrently \"npm run server\" \"npm run client\"",

        有

        "start": "npx run concurrently \"npm run server\" \"npm run client\"",

        npx 命令在您使用 npm 5.2 及更高版本时可用,让您无需在本地安装即可运行包

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-09
          • 2021-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-06
          • 2020-08-19
          相关资源
          最近更新 更多