簡體   English   中英

無法在 heroku 上部署我的 MERN 堆棧應用程序

[英]Unable to deploy my MERN stack app on heroku

我創建了一個簡單的 MERN 堆棧應用程序並嘗試在 heroku 上部署該應用程序,但是,應用程序在出現服務器端構建錯誤后崩潰。 我只是在學習 MERN stack 和 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,如果您能詳細解釋我做錯了什么以及如何糾正它,那就太好了。

您正在同時使用 npm 模塊啟動應用程序。 此模塊作為開發依賴項安裝,不會在生產中運行。 嘗試將其放在 package.json 文件中的依賴項下,並將其從 devDependencies 中刪除

錯誤的原因是您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文件。

從日志信息來看,問題似乎是 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版本起,讓你運行一個包,而無需本地安裝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM