在部署到 Heroku 之前,我应该对 MERN 应用程序的 package.json 文件的脚本进行哪些更改?

Posted

技术标签:

【中文标题】在部署到 Heroku 之前,我应该对 MERN 应用程序的 package.json 文件的脚本进行哪些更改?【英文标题】:What changes should I make in the script of the package.json file of a MERN app before deploying on Heroku? 【发布时间】:2020-12-24 16:02:00 【问题描述】:

我应该在 MERN 应用程序的根文件夹中的 package.json 文件的脚本中进行哪些更改,该应用程序还包含单独的 frontend_client 和 backend_server 子根文件夹?

现在我在根文件夹中的 package.json 文件如下所示,我已提交到远程 Heroku git,它已成功编译,但在启动应用程序时出现错误,如 Heroku 日志中所示


  "name": "root",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": 
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "concurrently \"cd backend_server && npm run dev\" \"cd frontend_client && npm start\"",
  ,
  "repository": 
    "type": "git",
    "url": "git+https://github.com/nishant-k-290195/p1.git"
  ,
  "author": "Nishant Kumar",
  "license": "ISC",
  "engines": 
    "node": "12.18.3"
  ,
  "bugs": 
    "url": "https://github.com/nishant-k-290195/p1/issues"
  ,
  "homepage": "https://github.com/nishant-k-290195/p1#readme",
  "devDependencies": 
    "concurrently": "^5.3.0"
  

以下是heroku日志-

2020-09-05T17:33:43.489116+00:00 heroku[web.1]: Process exited with status 1
2020-09-05T17:33:43.528008+00:00 heroku[web.1]: State changed from starting to crashed
2020-09-05T17:34:31.525583+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=fast-anchorage-34297.herokuapp.com request_id=57a25f1b-6d5b-4549-8f7b-8cd87dce4267 fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:34:32.283997+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=fast-anchorage-34297.herokuapp.com request_id=d8cd0ef2-d1db-4a8a-b3a1-0c4aca202e22 fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:41:22.000000+00:00 app[api]: Build started by user nishant290195.nkr@gmail.com
2020-09-05T17:41:42.000000+00:00 app[api]: Build failed -- check your build output: https://dashboard.heroku.com/apps/a283d1cf-d0fe-41b3-b8da-73c343355213/activity/builds/2e47b96c-e89d-46d3-8eeb-a045848df017
2020-09-05T17:55:39.000000+00:00 app[api]: Build started by user nishant290195.nkr@gmail.com
2020-09-05T17:55:54.797062+00:00 app[api]: Deploy c0d16916 by user nishant290195.nkr@gmail.com
2020-09-05T17:55:54.797062+00:00 app[api]: Release v4 created by user nishant290195.nkr@gmail.com
2020-09-05T17:55:55.000000+00:00 app[api]: Build succeeded
2020-09-05T17:55:58.414761+00:00 heroku[web.1]: State changed from crashed to starting
2020-09-05T17:56:00.690083+00:00 heroku[web.1]: Starting process with command `npm run dev`
2020-09-05T17:56:03.649588+00:00 app[web.1]: 
2020-09-05T17:56:03.649613+00:00 app[web.1]: > root@1.0.0 dev /app
2020-09-05T17:56:03.649614+00:00 app[web.1]: > concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"
2020-09-05T17:56:03.649615+00:00 app[web.1]: 
2020-09-05T17:56:03.664007+00:00 app[web.1]: sh: 1: concurrently: not found
2020-09-05T17:56:03.669157+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-09-05T17:56:03.669442+00:00 app[web.1]: npm ERR! syscall spawn
2020-09-05T17:56:03.669852+00:00 app[web.1]: npm ERR! file sh
2020-09-05T17:56:03.670220+00:00 app[web.1]: npm ERR! errno ENOENT
2020-09-05T17:56:03.673293+00:00 app[web.1]: npm ERR! root@1.0.0 dev: `concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"`
2020-09-05T17:56:03.673429+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-09-05T17:56:03.673587+00:00 app[web.1]: npm ERR!
2020-09-05T17:56:03.673750+00:00 app[web.1]: npm ERR! Failed at the root@1.0.0 dev script.
2020-09-05T17:56:03.673904+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-09-05T17:56:03.683841+00:00 app[web.1]: 
2020-09-05T17:56:03.684138+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-09-05T17:56:03.684329+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-09-05T17_56_03_675Z-debug.log
2020-09-05T17:56:03.737366+00:00 heroku[web.1]: Process exited with status 1
2020-09-05T17:56:03.780816+00:00 heroku[web.1]: State changed from starting to crashed
2020-09-05T17:56:03.783639+00:00 heroku[web.1]: State changed from crashed to starting
2020-09-05T17:56:05.855472+00:00 heroku[web.1]: Starting process with command `npm run dev`
2020-09-05T17:56:08.905828+00:00 app[web.1]: 
2020-09-05T17:56:08.905855+00:00 app[web.1]: > root@1.0.0 dev /app
2020-09-05T17:56:08.905855+00:00 app[web.1]: > concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"
2020-09-05T17:56:08.905856+00:00 app[web.1]: 
2020-09-05T17:56:08.925225+00:00 app[web.1]: sh: 1: concurrently: not found
2020-09-05T17:56:08.937713+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-09-05T17:56:08.938054+00:00 app[web.1]: npm ERR! syscall spawn
2020-09-05T17:56:08.938476+00:00 app[web.1]: npm ERR! file sh
2020-09-05T17:56:08.938852+00:00 app[web.1]: npm ERR! errno ENOENT
2020-09-05T17:56:08.943832+00:00 app[web.1]: npm ERR! root@1.0.0 dev: `concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"`
2020-09-05T17:56:08.943985+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-09-05T17:56:08.944141+00:00 app[web.1]: npm ERR!
2020-09-05T17:56:08.944283+00:00 app[web.1]: npm ERR! Failed at the root@1.0.0 dev script.
2020-09-05T17:56:08.944404+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-09-05T17:56:08.954584+00:00 app[web.1]: 
2020-09-05T17:56:08.954809+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-09-05T17:56:08.954944+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-09-05T17_56_08_946Z-debug.log
2020-09-05T17:56:09.036221+00:00 heroku[web.1]: Process exited with status 1
2020-09-05T17:56:09.078574+00:00 heroku[web.1]: State changed from starting to crashed
2020-09-05T17:56:11.011048+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=fast-anchorage-34297.herokuapp.com request_id=35e7708e-0cc1-45cb-926f-623d4cde16b0 fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:56:11.656448+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=fast-anchorage-34297.herokuapp.com request_id=8945408f-d80e-4d57-8ffc-f3bc1e33a8ad fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:59:12.714272+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=fast-anchorage-34297.herokuapp.com request_id=c2d646c9-f779-488f-b272-29320f87a76d fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:59:13.260112+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=fast-anchorage-34297.herokuapp.com request_id=445d6125-a6fc-4af6-975c-737091123a89 fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:59:15.753473+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=fast-anchorage-34297.herokuapp.com request_id=4f4f07df-066e-4eef-b2af-c19b0654f212 fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T17:59:16.257264+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=fast-anchorage-34297.herokuapp.com request_id=6f763484-4a76-40b1-a48f-a8b58b1ddc67 fwd="183.83.146.132" dyno= connect= service= status=503 bytes= protocol=https
2020-09-05T18:19:40.404117+00:00 heroku[web.1]: State changed from crashed to starting
2020-09-05T18:19:42.639891+00:00 heroku[web.1]: Starting process with command `npm run dev`
2020-09-05T18:19:45.146091+00:00 app[web.1]: 
2020-09-05T18:19:45.146144+00:00 app[web.1]: > root@1.0.0 dev /app
2020-09-05T18:19:45.146148+00:00 app[web.1]: > concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"
2020-09-05T18:19:45.146148+00:00 app[web.1]: 
2020-09-05T18:19:45.154837+00:00 app[web.1]: sh: 1: concurrently: not found
2020-09-05T18:19:45.159721+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-09-05T18:19:45.159910+00:00 app[web.1]: npm ERR! syscall spawn
2020-09-05T18:19:45.160045+00:00 app[web.1]: npm ERR! file sh
2020-09-05T18:19:45.160228+00:00 app[web.1]: npm ERR! errno ENOENT
2020-09-05T18:19:45.162886+00:00 app[web.1]: npm ERR! root@1.0.0 dev: `concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"`
2020-09-05T18:19:45.162963+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-09-05T18:19:45.163063+00:00 app[web.1]: npm ERR!
2020-09-05T18:19:45.163163+00:00 app[web.1]: npm ERR! Failed at the root@1.0.0 dev script.
2020-09-05T18:19:45.163319+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-09-05T18:19:45.171480+00:00 app[web.1]: 
2020-09-05T18:19:45.171611+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-09-05T18:19:45.171645+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-09-05T18_19_45_164Z-debug.log
2020-09-05T18:19:45.226610+00:00 heroku[web.1]: Process exited with status 1
2020-09-05T18:19:45.261608+00:00 heroku[web.1]: State changed from starting to crashed
2020-09-05T19:10:59.294228+00:00 heroku[web.1]: State changed from crashed to starting
2020-09-05T19:11:01.544665+00:00 heroku[web.1]: Starting process with command `npm run dev`
2020-09-05T19:11:04.141984+00:00 app[web.1]: 
2020-09-05T19:11:04.142014+00:00 app[web.1]: > root@1.0.0 dev /app
2020-09-05T19:11:04.142015+00:00 app[web.1]: > concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"
2020-09-05T19:11:04.142015+00:00 app[web.1]: 
2020-09-05T19:11:04.149729+00:00 app[web.1]: sh: 1: concurrently: not found
2020-09-05T19:11:04.157981+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-09-05T19:11:04.158322+00:00 app[web.1]: npm ERR! syscall spawn
2020-09-05T19:11:04.158560+00:00 app[web.1]: npm ERR! file sh
2020-09-05T19:11:04.158822+00:00 app[web.1]: npm ERR! errno ENOENT
2020-09-05T19:11:04.163828+00:00 app[web.1]: npm ERR! root@1.0.0 dev: `concurrently "cd backend_server && npm run dev" "cd frontend_client && npm start"`
2020-09-05T19:11:04.163980+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-09-05T19:11:04.164157+00:00 app[web.1]: npm ERR!
2020-09-05T19:11:04.164316+00:00 app[web.1]: npm ERR! Failed at the root@1.0.0 dev script.
2020-09-05T19:11:04.164471+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-09-05T19:11:04.183322+00:00 app[web.1]: 
2020-09-05T19:11:04.183591+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-09-05T19:11:04.183760+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-09-05T19_11_04_174Z-debug.log
2020-09-05T19:11:04.263772+00:00 heroku[web.1]: Process exited with status 1
2020-09-05T19:11:04.328653+00:00 heroku[web.1]: State changed from starting to crashed

【问题讨论】:

1.什么错误? 2. 该根包文件如何挂钩到 Heroku 调用的任何脚本(基本上:安装、构建、修剪、启动)? 请edit问题包含相关细节。 我已经添加了日志,请告诉我您需要什么详细信息?您需要我的文件夹结构或特定文件中的代码吗? Heroku 在构建后剥离(“修剪”)您的开发依赖项,因此在完成的测功机中同时不可用。但是无论如何,您都不应该在 prod 中运行开发脚本;这是 dev 脚本。 如果您的意思是删除开发脚本,我将删除它,但我应该添加什么代码?我在根文件夹的两个单独的子文件夹中有反应应用程序和节点应用程序。所以我不能只在 package.json 中添加“start”:“node app.js”脚本。我现在该怎么办? 【参考方案1】: 后端的package.json 根据您的文件夹结构替换脚本中的文件位置 这里 client 目录有 react 代码,后端代码在根目录中。
    "start": "NODE_ENV=production node server.js",
    "client": "npm start --prefix client",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
server.js
const path = require('path');

if (process.env.NODE_ENV === 'production') 
  // Set static folder
  app.use(express.static('client/build'));

  app.get('*', (req, res) => 
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  );

【讨论】:

以上是关于在部署到 Heroku 之前,我应该对 MERN 应用程序的 package.json 文件的脚本进行哪些更改?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在将 MERN 部署到 Heroku 期间错误地收到文件丢失错误?

尝试将带有顺风的 MERN 堆栈部署到 Heroku 时构建失败

部署到 heroku - 白屏 - 没有明显错误 - MERN 堆栈在本地工作得很好,包括 API 请求 - 已经尝试了 27 次部署

如何使用后端文件夹内的前端将 MERN 应用程序部署到 Heroku

在 heroku 上部署 MERN 会导致意外错误

如何在单个液滴上将不同的 MERN 应用程序部署到数字海洋?