简体   繁体   English

如何在 PM2 启动之前(之前)要求 dotenv/config 文件

[英]How to require dotenv/config file on (before) PM2 starts

I run node app like this:我像这样运行节点应用程序:

node -r dotenv/config dist/app

I need something similar using PM2:我需要使用 PM2 类似的东西:

pm2 start -r dotenv/config dist/app.js --name appname // doesn't work

I receive the next error: error: unknown option -r我收到下一个错误: error: unknown option -r

使用node_args

pm2 start --node-args="-r dotenv/config" dist/app.js --name appname

I have made a Shell script: 我做了一个Shell脚本:

// pm2-start.sh

NODE_ENV=production &&
node -r dotenv/config dist/app

Then I ran pm2 start pm2-start.sh --name appname 然后我运行pm2 start pm2-start.sh --name appname

Tip I have also ran: pm2 startup then copied command that pm2 instructed to run in order to activate the auto startup of all apps that are registered via pm2. 提示我还运行了: pm2 startup然后复制了pm2指示运行的命令,以激活通过pm2注册的所有应用程序的自动启动。

Then I ran pm2 save to save the auto service. 然后我运行pm2 save来保存自动服务。

Note: pm2 lists apps distinctly between server account respectively. 注意:pm2分别在服务器帐户之间列出应用程序。 That means that apps that are listed on user A will not be listed on user B. That's true for the pm2 startup command - that should be done for each account. 这意味着用户A上列出的应用程序将不会在用户B上列出。对于pm2 startup命令,这是正确的-应该为每个帐户完成。

Hope it helps. 希望能帮助到你。

None of this worked for me because I was using an ecosystem file AND cluster mode which behaves really odd (not like without cluster mode...).这些都不适合我,因为我使用的是生态系统文件和集群模式,它的行为非常奇怪(不像没有集群模式......)。

I installed dotenv as dev dependency at the root (I was using yarn workspaces too).我在根目录中安装了 dotenv 作为 dev 依赖项(我也使用了 yarn 工作区)。

Then I did this:然后我这样做了:

require('dotenv').config({ path: 'path/to/your/.env' })

module.exports = {
    apps: [
        {
            name: 'app',
            script: 'server/dist/index.js',
            instances: 2,
            exec_mode: 'cluster',
            instance_var: 'APP_INSTANCE_SEQ',
            // listen_timeout: 10000,
            // restart_delay: 10000,
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM