如何修复 npm ERR!将 MEAN 应用程序部署到 Heroku 时代码 ELIFECYCLE
How to fix npm ERR! code ELIFECYCLE when deploying MEAN app to Heroku
我在将后端部署到 Heroku 时遇到问题。我将 MEAN 堆栈与 Mongo Atlas 数据库一起使用。该应用程序在本地运行良好。但是一旦我部署它,它就会崩溃并显示 npm ERR! code ELIFECYCLE
。显然,Mongo Atlas 要求您将 IP 地址列入白名单。我读过的一篇文章说,如果我将连接字符串添加到 Heroku 环境变量中,它会很好地工作。所以我这样做了,并用名称 connectionString 保存了它。但是,这没有用。连接字符串是否需要特定名称?我在下面发布了我的 Heroku 日志。请注意,我已尝试删除我的 node_modules 和 package.json 并重新安装它们,并将我的节点版本更改为 10.x.
2021-03-17T07:22:23.865572+00:00 app[api]: Deploy 2cbf3bbb by user example@example.com
2021-03-17T07:22:23.865572+00:00 app[api]: Release v9 created by user example@example.com
2021-03-17T07:22:24.515688+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-17T07:22:25.000000+00:00 app[api]: Build succeeded
2021-03-17T07:22:27.638401+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-17T07:22:30.298270+00:00 app[web.1]:
2021-03-17T07:22:30.298283+00:00 app[web.1]: > backend@1.1.1 start /app
2021-03-17T07:22:30.298284+00:00 app[web.1]: > node index.js
2021-03-17T07:22:30.298284+00:00 app[web.1]:
2021-03-17T07:22:31.387500+00:00 app[web.1]: WARNING: NODE_ENV value of 'production' did not match any deployment config file names.
2021-03-17T07:22:31.387844+00:00 app[web.1]: WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
2021-03-17T07:22:31.931808+00:00 heroku[web.1]: State changed from starting to up
2021-03-17T07:23:01.761238+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-17T07:23:01.761850+00:00 app[web.1]: npm ERR! errno 1
2021-03-17T07:23:01.770367+00:00 app[web.1]: npm ERR! backend@1.1.1 start: `node index.js`
2021-03-17T07:23:01.770621+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-17T07:23:01.770728+00:00 app[web.1]: npm ERR!
2021-03-17T07:23:01.772482+00:00 app[web.1]: npm ERR! Failed at the backend@1.1.1 start script.
2021-03-17T07:23:01.772597+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-17T07:23:01.781656+00:00 app[web.1]:
2021-03-17T07:23:01.781807+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-17T07:23:01.781869+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-03-17T07_23_01_773Z-debug.log
2021-03-17T07:23:01.841833+00:00 heroku[web.1]: Process exited with status 1
2021-03-17T07:23:01.965241+00:00 heroku[web.1]: State changed from up to crashed
2021-03-17T07:23:01.973670+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-17T07:23:06.945061+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-17T07:23:11.079200+00:00 app[web.1]:
2021-03-17T07:23:11.079239+00:00 app[web.1]: > backend@1.1.1 start /app
2021-03-17T07:23:11.079239+00:00 app[web.1]: > node index.js
2021-03-17T07:23:11.079239+00:00 app[web.1]:
2021-03-17T07:23:12.636407+00:00 app[web.1]: WARNING: NODE_ENV value of 'production' did not match any deployment config file names.
2021-03-17T07:23:12.639137+00:00 app[web.1]: WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
2021-03-17T07:23:13.442612+00:00 heroku[web.1]: State changed from starting to up
2021-03-17T07:23:43.404828+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-17T07:23:43.405294+00:00 app[web.1]: npm ERR! errno 1
2021-03-17T07:23:43.419129+00:00 app[web.1]: npm ERR! backend@1.1.1 start: `node index.js`
2021-03-17T07:23:43.419414+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-17T07:23:43.419741+00:00 app[web.1]: npm ERR!
2021-03-17T07:23:43.420082+00:00 app[web.1]: npm ERR! Failed at the backend@1.1.1 start script.
2021-03-17T07:23:43.420380+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-17T07:23:43.437105+00:00 app[web.1]:
2021-03-17T07:23:43.439175+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-17T07:23:43.439472+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-03-17T07_23_43_421Z-debug.log
2021-03-17T07:23:43.562438+00:00 heroku[web.1]: Process exited with status 1
2021-03-17T07:23:43.638872+00:00 heroku[web.1]: State changed from up to crashed```
所以我想出了问题。连接到我的数据库时,我使用配置模块加载 Mongodb Atlas 连接字符串。
mongoose.connect(config.get('configurationString'), {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true}).then(() => winston.info("Connected to MongoDB..."))
我将 config.get('connectionString')
更改为 process.env.MONGODB_URL
,然后使用 heroku config:set MONGODB_URL="<YOUR MONGODB ATLAS CONNECTION STRING>"
将 MONGODB_URL env 变量添加到 heroku。还要确保你有一个 Procfile 并添加行 web:node index.js
我在将后端部署到 Heroku 时遇到问题。我将 MEAN 堆栈与 Mongo Atlas 数据库一起使用。该应用程序在本地运行良好。但是一旦我部署它,它就会崩溃并显示 npm ERR! code ELIFECYCLE
。显然,Mongo Atlas 要求您将 IP 地址列入白名单。我读过的一篇文章说,如果我将连接字符串添加到 Heroku 环境变量中,它会很好地工作。所以我这样做了,并用名称 connectionString 保存了它。但是,这没有用。连接字符串是否需要特定名称?我在下面发布了我的 Heroku 日志。请注意,我已尝试删除我的 node_modules 和 package.json 并重新安装它们,并将我的节点版本更改为 10.x.
2021-03-17T07:22:23.865572+00:00 app[api]: Deploy 2cbf3bbb by user example@example.com
2021-03-17T07:22:23.865572+00:00 app[api]: Release v9 created by user example@example.com
2021-03-17T07:22:24.515688+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-17T07:22:25.000000+00:00 app[api]: Build succeeded
2021-03-17T07:22:27.638401+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-17T07:22:30.298270+00:00 app[web.1]:
2021-03-17T07:22:30.298283+00:00 app[web.1]: > backend@1.1.1 start /app
2021-03-17T07:22:30.298284+00:00 app[web.1]: > node index.js
2021-03-17T07:22:30.298284+00:00 app[web.1]:
2021-03-17T07:22:31.387500+00:00 app[web.1]: WARNING: NODE_ENV value of 'production' did not match any deployment config file names.
2021-03-17T07:22:31.387844+00:00 app[web.1]: WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
2021-03-17T07:22:31.931808+00:00 heroku[web.1]: State changed from starting to up
2021-03-17T07:23:01.761238+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-17T07:23:01.761850+00:00 app[web.1]: npm ERR! errno 1
2021-03-17T07:23:01.770367+00:00 app[web.1]: npm ERR! backend@1.1.1 start: `node index.js`
2021-03-17T07:23:01.770621+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-17T07:23:01.770728+00:00 app[web.1]: npm ERR!
2021-03-17T07:23:01.772482+00:00 app[web.1]: npm ERR! Failed at the backend@1.1.1 start script.
2021-03-17T07:23:01.772597+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-17T07:23:01.781656+00:00 app[web.1]:
2021-03-17T07:23:01.781807+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-17T07:23:01.781869+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-03-17T07_23_01_773Z-debug.log
2021-03-17T07:23:01.841833+00:00 heroku[web.1]: Process exited with status 1
2021-03-17T07:23:01.965241+00:00 heroku[web.1]: State changed from up to crashed
2021-03-17T07:23:01.973670+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-17T07:23:06.945061+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-17T07:23:11.079200+00:00 app[web.1]:
2021-03-17T07:23:11.079239+00:00 app[web.1]: > backend@1.1.1 start /app
2021-03-17T07:23:11.079239+00:00 app[web.1]: > node index.js
2021-03-17T07:23:11.079239+00:00 app[web.1]:
2021-03-17T07:23:12.636407+00:00 app[web.1]: WARNING: NODE_ENV value of 'production' did not match any deployment config file names.
2021-03-17T07:23:12.639137+00:00 app[web.1]: WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
2021-03-17T07:23:13.442612+00:00 heroku[web.1]: State changed from starting to up
2021-03-17T07:23:43.404828+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-17T07:23:43.405294+00:00 app[web.1]: npm ERR! errno 1
2021-03-17T07:23:43.419129+00:00 app[web.1]: npm ERR! backend@1.1.1 start: `node index.js`
2021-03-17T07:23:43.419414+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-17T07:23:43.419741+00:00 app[web.1]: npm ERR!
2021-03-17T07:23:43.420082+00:00 app[web.1]: npm ERR! Failed at the backend@1.1.1 start script.
2021-03-17T07:23:43.420380+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-17T07:23:43.437105+00:00 app[web.1]:
2021-03-17T07:23:43.439175+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-17T07:23:43.439472+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-03-17T07_23_43_421Z-debug.log
2021-03-17T07:23:43.562438+00:00 heroku[web.1]: Process exited with status 1
2021-03-17T07:23:43.638872+00:00 heroku[web.1]: State changed from up to crashed```
所以我想出了问题。连接到我的数据库时,我使用配置模块加载 Mongodb Atlas 连接字符串。
mongoose.connect(config.get('configurationString'), {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true}).then(() => winston.info("Connected to MongoDB..."))
我将 config.get('connectionString')
更改为 process.env.MONGODB_URL
,然后使用 heroku config:set MONGODB_URL="<YOUR MONGODB ATLAS CONNECTION STRING>"
将 MONGODB_URL env 变量添加到 heroku。还要确保你有一个 Procfile 并添加行 web:node index.js