首页 > 解决方案 > 无法将 MERN 应用程序推送到 heroku 服务器

问题描述

因此,我将这个 MERN 应用程序推送到我的 github 存储库https://github.com/Banialczele/coursemanagement,现在我想将该存储库推送到 heroku。
我的问题是,当我尝试将我的应用程序部署到 heroku 服务器时,它会部署到服务器,但是当我想访问该链接时出现错误。
但是,当我尝试heroku local web在我的 IDE 中运行时,我的应用程序运行完美,但是当我想部署到服务器时,某些东西一直在崩溃。这是堆栈跟踪的样子:

2020-03-14T12:25:20.748376+00:00 app[web.1]: [1] Error: Cannot find module 'express'
2020-03-14T12:25:20.748376+00:00 app[web.1]: [1] Require stack:
2020-03-14T12:25:20.748376+00:00 app[web.1]: [1] - /app/server/src/index.js
2020-03-14T12:25:20.748378+00:00 app[web.1]: [1]     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
2020-03-14T12:25:20.748379+00:00 app[web.1]: [1]     at Function.Module._load (internal/modules/cjs/loader.js:864:27)
2020-03-14T12:25:20.748379+00:00 app[web.1]: [1]     at Module.require (internal/modules/cjs/loader.js:1044:19)
2020-03-14T12:25:20.748380+00:00 app[web.1]: [1]     at require (internal/modules/cjs/helpers.js:77:18)
2020-03-14T12:25:20.748380+00:00 app[web.1]: [1]     at Object.<anonymous> (/app/server/src/index.js:1:17)
2020-03-14T12:25:20.748380+00:00 app[web.1]: [1]     at Module._compile (internal/modules/cjs/loader.js:1158:30)
2020-03-14T12:25:20.748380+00:00 app[web.1]: [1]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
2020-03-14T12:25:20.748381+00:00 app[web.1]: [1]     at Module.load (internal/modules/cjs/loader.js:1002:32)
2020-03-14T12:25:20.748381+00:00 app[web.1]: [1]     at Function.Module._load (internal/modules/cjs/loader.js:901:14)
2020-03-14T12:25:20.748381+00:00 app[web.1]: [1]     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
2020-03-14T12:25:20.748381+00:00 app[web.1]: [1]   code: 'MODULE_NOT_FOUND',
2020-03-14T12:25:20.748382+00:00 app[web.1]: [1]   requireStack: [ '/app/server/src/index.js' ]
2020-03-14T12:25:20.748382+00:00 app[web.1]: [1] }
2020-03-14T12:25:20.756503+00:00 app[web.1]: [1] npm ERR! code ELIFECYCLE
2020-03-14T12:25:20.756505+00:00 app[web.1]: [1] npm ERR! errno 1
2020-03-14T12:25:20.759707+00:00 app[web.1]: [1] npm ERR! teachercourseapp@1.0.0 server: `cd server/src && node index.js`
2020-03-14T12:25:20.760641+00:00 app[web.1]: [1] npm ERR! Exit status 1
2020-03-14T12:25:20.760642+00:00 app[web.1]: [1] npm ERR!
2020-03-14T12:25:20.760643+00:00 app[web.1]: [1] npm ERR! Failed at the teachercourseapp@1.0.0 server script.
2020-03-14T12:25:20.760644+00:00 app[web.1]: [1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-03-14T12:25:20.769172+00:00 app[web.1]: [1]
2020-03-14T12:25:20.771168+00:00 app[web.1]: [1] npm ERR! A complete log of this run can be found in:
2020-03-14T12:25:20.771169+00:00 app[web.1]: [1] npm ERR!     /app/.npm/_logs/2020-03-14T12_25_20_761Z-debug.log
2020-03-14T12:25:20.779534+00:00 app[web.1]: [1] npm run server exited with code 1
2020-03-14T12:25:21.019553+00:00 app[web.1]: [0]
2020-03-14T12:25:21.019562+00:00 app[web.1]: [0] > coursemanagement@0.1.0 start /app/client
2020-03-14T12:25:21.019563+00:00 app[web.1]: [0] > react-scripts start
2020-03-14T12:25:21.019563+00:00 app[web.1]: [0]
2020-03-14T12:25:21.024669+00:00 app[web.1]: [0] sh: 1: react-scripts: not found
2020-03-14T12:25:21.029965+00:00 app[web.1]: [0] npm ERR! code ELIFECYCLE
2020-03-14T12:25:21.029966+00:00 app[web.1]: [0] npm ERR! syscall spawn
2020-03-14T12:25:21.030363+00:00 app[web.1]: npm ERR! file sh
2020-03-14T12:25:21.030364+00:00 app[web.1]: [0] npm ERR! errno ENOENT
2020-03-14T12:25:21.031948+00:00 app[web.1]: [0] npm ERR! coursemanagement@0.1.0 start: `react-scripts start`
2020-03-14T12:25:21.031949+00:00 app[web.1]: [0] npm ERR! spawn ENOENT
2020-03-14T12:25:21.031949+00:00 app[web.1]: [0] npm ERR!
2020-03-14T12:25:21.033375+00:00 app[web.1]: npm ERR! Failed at the coursemanagement@0.1.0 start script.
2020-03-14T12:25:21.033376+00:00 app[web.1]: [0] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-03-14T12:25:21.037820+00:00 app[web.1]: [0] npm WARN Local package.json exists, but node_modules missing, did you mean to install?
2020-03-14T12:25:21.038198+00:00 app[web.1]: [0]
2020-03-14T12:25:21.038591+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-03-14T12:25:21.038592+00:00 app[web.1]: [0] npm ERR!     /app/.npm/_logs/2020-03-14T12_25_21_032Z-debug.log
2020-03-14T12:25:21.047517+00:00 app[web.1]: [0] npm ERR! code ELIFECYCLE
2020-03-14T12:25:21.047518+00:00 app[web.1]: [0] npm ERR! errno 1
2020-03-14T12:25:21.048896+00:00 app[web.1]: [0] npm ERR! teachercourseapp@1.0.0 client: `cd client/src && npm start`
2020-03-14T12:25:21.048897+00:00 app[web.1]: [0] npm ERR! Exit status 1
2020-03-14T12:25:21.048897+00:00 app[web.1]: [0] npm ERR!
2020-03-14T12:25:21.049322+00:00 app[web.1]: npm ERR! Failed at the teachercourseapp@1.0.0 client script.
2020-03-14T12:25:21.049753+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-03-14T12:25:21.055102+00:00 app[web.1]: [0]
2020-03-14T12:25:21.055708+00:00 app[web.1]: [0] npm ERR! A complete log of this run can be found in:
2020-03-14T12:25:21.055709+00:00 app[web.1]: [0] npm ERR!     /app/.npm/_logs/2020-03-14T12_25_21_050Z-debug.log
2020-03-14T12:25:21.060294+00:00 app[web.1]: [0] npm run client exited with code 1
2020-03-14T12:25:21.067946+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-03-14T12:25:21.068281+00:00 app[web.1]: npm ERR! errno 1
2020-03-14T12:25:21.069285+00:00 app[web.1]: npm ERR! teachercourseapp@1.0.0 start: `concurrently "npm run client" "npm run server" `
2020-03-14T12:25:21.069440+00:00 app[web.1]: npm ERR! Exit status 1
2020-03-14T12:25:21.069684+00:00 app[web.1]: npm ERR!
2020-03-14T12:25:21.069864+00:00 app[web.1]: npm ERR! Failed at the teachercourseapp@1.0.0 start script.
2020-03-14T12:25:21.070045+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-03-14T12:25:21.075605+00:00 app[web.1]:
2020-03-14T12:25:21.075900+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-03-14T12:25:21.076095+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-03-14T12_25_21_070Z-debug.log
2020-03-14T12:25:21.194915+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-14T12:25:21.175761+00:00 heroku[web.1]: Process exited with status 1
2020-03-14T12:25:22.079114+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=managingcourses.herokuapp.com request_id=175c1aa8-0966-4493-a328-ed303e4b8075 fwd="188.123.215.228
" dyno= connect= service= status=503 bytes= protocol=https
2020-03-14T12:25:22.577434+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=managingcourses.herokuapp.com request_id=912f071c-c6c0-49ce-996d-33005e324fb1 fwd="188.
123.215.228" dyno= connect= service= status=503 bytes= protocol=https

它以找不到express模块开始,但我用 --save 标志重新安装了它,没有任何变化。你能帮我解决这个问题吗?我被这个错误堆叠了 2 天。

标签: node.jsherokunpm

解决方案


在评论之后,我发布了问题所在:Heroku 正在查看package.json位于您项目的根目录。您在服务器文件夹中安装了 express,但根目录中的文件不包含它。

您可以推送存储库的子树:

git subtree push --prefix path/to/subdirectory heroku master

或者只是将您的服务器和客户端部分拆分为两个存储库。


推荐阅读