我正在将 MERN 堆栈应用程序部署到 Heroku。在本地运行时,我的项目运行良好,但是当我尝试从 Heroku 运行我的应用程序时遇到以下错误。
2020-11-23T01:08:02.199575+00:00 app[web.1]: npm ERR! Failed at the mernshoppinglist@1.0.0 start script.
这是我尝试加载应用程序时来自 Heroku 的完整日志
2020-11-23T01:08:02.199331+00:00 app[web.1]: npm ERR!
2020-11-23T01:08:02.199575+00:00 app[web.1]: npm ERR! Failed at the mernshoppinglist@1.0.0 start script.
2020-11-23T01:08:02.199766+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-11-23T01:08:02.875811+00:00 app[web.1]:
2020-11-23T01:08:02.876018+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-11-23T01:08:02.876125+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-11-23T01_08_02_200Z-debug.log
2020-11-23T01:08:02.935584+00:00 heroku[web.1]: Process exited with status 1
2020-11-23T01:08:02.975882+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-23T01:08:03.765808+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=reke-mern-shopping-list.herokuapp.com request_id=0992d8aa-8716-407f-a59e-df58e9e39a54 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
2020-11-23T01:08:04.134057+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=reke-mern-shopping-list.herokuapp.com request_id=ba4d4159-75c3-458c-b3f1-cc1168ac4ca7 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
当我研究这个错误时,有人建议我在 package.json 中添加一个启动脚本。但是,我的 package.json 中有它。我很好奇是否有人会知道解决方案,或者同样重要的是,我应该采取哪些步骤来解决这个问题。 Heroku 部署对我来说是一场真正的斗争。你可以在下面看到完整的文件{
"name": "mernshoppinglist",
"version": "1.0.0",
"description": "Shopping list with with the MERN stack and JWT",
"main": "server.js",
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "David REke",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"concurrently": "^5.3.0",
"config": "^3.3.2",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.10"
}
}
我正在跟随一个教程,我能说的最好的我已经匹配了作者的规定。我不确定是否需要它,但下面也是我的 server.js 文件。const express = require('express');
const mongoose = require('mongoose');
const app = express();
const path = require('path');
const config = require('config')
// Bodyparser Middleware
app.use(express.json());
// DB Config
const db = config.get('mongoURI');
// connect to MongoDB
mongoose.connect(db, {useNewUrlParser: true,
useCreateIndex: true})
.then(() => {
console.log('mongoDB Connected...')
})
.catch((err) => {
console.log(err)
});
// use Route
app.use('/api/items', require('./routes/api/items'));
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'))
// added code from youtube comments
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));
// Server static assets if in production
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'));
} )
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`server started on port ${port}`))
编辑:我应该补充一点,我没有 procfile,但我的理解是我不需要一个,因为我的 package.json 中有一个启动脚本。编辑 2:当我实现 chmsv 的修复时,我在日志中收到以下错误:
2020-11-24T13:45:26.235249+00:00 app[worker.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
2020-11-24T13:45:26.235249+00:00 app[worker.1]: at Module.load (internal/modules/cjs/loader.js:879:32)
2020-11-24T13:45:26.235250+00:00 app[worker.1]: at Function.Module._load (internal/modules/cjs/loader.js:724:14)
2020-11-24T13:45:26.235250+00:00 app[worker.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
2020-11-24T13:45:26.235251+00:00 app[worker.1]: at internal/main/run_main_module.js:17:47 {
2020-11-24T13:45:26.235251+00:00 app[worker.1]: code: 'MODULE_NOT_FOUND',
2020-11-24T13:45:26.235251+00:00 app[worker.1]: requireStack: [ '/app/server.js' ]
2020-11-24T13:45:26.235252+00:00 app[worker.1]: }
2020-11-24T13:45:26.307299+00:00 heroku[worker.1]: Process exited with status 1
2020-11-24T13:45:26.357136+00:00 heroku[worker.1]: State changed from up to crashed
最佳答案
部署后,Heroku 将在你的 package.json 中运行命令 "start": "node server.js"。但无论如何,Heroku 也需要 Procfile。
worker: node server.js
.如果您使用 Github 进行部署,请不要忘记推送更改。 关于node.js - Heroku 在启动脚本中失败,但我的 package.json 中有一个标准的启动脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64961432/