Dotenv: Env variables undefined when starting with pm2

Created on 25 Sep 2017  路  19Comments  路  Source: motdotla/dotenv

It seems like dotenv doesn't work when you start it with pm2. Is this a known issue?

Most helpful comment

This package does work with PM2, I'm doing it in dev right now. Make sure to set cwd in the pm2 config to the correct directory for any calls to dotenv.config().

Example:
Your index.js file is in /app/src, your .env file is in /app. Your index.js file has this

dotenv.config({path: "../.env"});

Your pm2 json config should have this:
"cwd": "/app/src", "script": "index.js"

You could also use dotenv.config({path: path.join(__dirname, "../.env")}); to avoid the CWD issue. You will still have a problem if you move the .env or the index.js file relative to each other.

All 19 comments

I have a hunch this is a configuration issue. Try configuring dotenv with an absolute path to your .env file. Alternatively, you can specify environment variables in the pm2 config

Absolute path doesn't seem to work.

This package does work with PM2, I'm doing it in dev right now. Make sure to set cwd in the pm2 config to the correct directory for any calls to dotenv.config().

Example:
Your index.js file is in /app/src, your .env file is in /app. Your index.js file has this

dotenv.config({path: "../.env"});

Your pm2 json config should have this:
"cwd": "/app/src", "script": "index.js"

You could also use dotenv.config({path: path.join(__dirname, "../.env")}); to avoid the CWD issue. You will still have a problem if you move the .env or the index.js file relative to each other.

@kingjerod Won't work in pm2 cluster mode

Pm2 has its own setup file to inject environment variables. http://pm2.keymetrics.io/docs/usage/environment/

Once you define the environment, dotenv will work as usual.

@Leland-Kwong yes but we use the .env file to not store our variables in code. Doing like this exposes them.

@ritesh-nitw did you ever figure this out? Works for me when running pm2-dev but not pm2 in cluster mode.

dotenv works when I start the app at first time, but every environment variables in .env becomes undefined when I restart app. Any updates?

Is the restart occuring in the same folder?

@semiautomatix Yes, it is. When I move all environments variables to ecosystem.config.js which is config file of pm2 will works all right.
My file structure:

 - www
...
app.js
...
.env
ecosystem.config.js
...

Looks like if you use a path like this then it doesn't work:

pm2 start /path/to/myapp/blah.js

So you need to navigate to the root dir where your file lives and run the command like:

pm2 start blah.js

This seems to work correctly for me

This does not work for me with [email protected].

I have two apis, each with a root server.js and a root .env. The app works fine with nodemon and proper environment is detected, but with pm2 both apis are sharing one of the .env config files.

This does not work for me with [email protected].

I have two apis, each with a root server.js and a root .env. The app works fine with nodemon and proper environment is detected, but with pm2 both apis are sharing one of the .env config files.

I have same issue

@chovy
you should add --env production, and you can get env variables.

can confirm this still doesn't work...

im running:

pm2-runtime --node-args="--require dotenv/config" start process.yml

and the apps dont seem to pickup the .env values

For me this worked-

pm2 start ecosystem.config.js server.js --node-args="--require dotenv/config" --env production

by using pm2 ecosystem one can generate the ecosystem.config.js file

can confirm this still doesn't work...

im running:

pm2-runtime --node-args="--require dotenv/config" start process.yml

and the apps dont seem to pickup the .env values

Putting node-args config into ecosystem.config.js can works, see node_args in https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

Environment variables worked for me when I started pm2 inside the project folder

if your'e in docker use pm2-runtime

"start": "npx pm2-runtime index.js --name='hire-api' --watch",

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ycmjason picture ycmjason  路  4Comments

shai32 picture shai32  路  5Comments

tikotzky picture tikotzky  路  4Comments

AndrewChen1982 picture AndrewChen1982  路  4Comments

marcusradell picture marcusradell  路  3Comments