简体   繁体   中英

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 start -r dotenv/config dist/app.js --name appname // doesn't work

I receive the next error: error: unknown option -r

使用node_args

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

I have made a Shell script:

// pm2-start.sh

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

Then I ran 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.

Then I ran pm2 save to save the auto service.

Note: pm2 lists apps distinctly between server account respectively. 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.

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).

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,
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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