微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用 pm2

如何解决无法使用 pm2

我已经成功地使用如下所示的基本 npm 启动脚本在 ElasticBeanstalk 上运行我的应用程序。

"scripts": {
    "start": "NODE_ENV=production node ./bin/www"
}

在我的服务器上,我现在全局安装了 pm2 并更新了 start 中的 package.json 脚本以使用 pm2。

"scripts": {
    "start": "pm2 start ./bin/www"
}

pm2 启动进程,我可以通过查看我的日志看到这一点,但是它似乎无法访问我为 ElasticBeanstalk 设置的环境变量。我在文件 .ebextensions/options.config 中使用 aws:elasticbeanstalk:application:environment 选项设置了环境变量,如下所示。

option_settings:
  aws:elasticbeanstalk:application:environment:
    keyOne: foo
    keyTwo: bar
    keyThree: foo

pm2 抱怨无法读取环境变量的 undefined 属性

0|www    | TypeError: Cannot read property 'keyOne' of undefined
0|www    |     at Object.<anonymous> (/var/app/current/app.js:26:38)
0|www    |     at Module._compile (internal/modules/cjs/loader.js:999:30)
0|www    |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
0|www    |     at Module.load (internal/modules/cjs/loader.js:863:32)
0|www    |     at Function.Module._load (internal/modules/cjs/loader.js:708:14)
0|www    |     at Module.require (internal/modules/cjs/loader.js:887:19)
0|www    |     at Module.Hook._require.Module.require (/var/app/current/node_modules/require-in-the-middle/index.js:80:39)
0|www    |     at require (internal/modules/cjs/helpers.js:74:18)
0|www    |     at Object.<anonymous> (/var/app/current/bin/www:7:11)
0|www    |     at Module._compile (internal/modules/cjs/loader.js:999:30)

我需要先获取文件吗?我想避免在 pm2 的生态系统.config.js 文件中设置环境变量,因为我现在还可以使用当前实施的方法从 AWS 控制台以可视方式管理环境变量。

解决方法

如果你已经创建了 PM2 进程,然后更新了环境变量,你应该删除 PM2 进程并使用生态系统文件重新创建它。

删除和创建操作考虑新创建的环境变量,但不考虑重启。我不知道为什么。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。