9

I'm building out a nodejs api and have setup the dotenv package as a dev dependency to load the variables to process.env on developer's local machines.

Note that when I log in I use sudo -i to operate as root.

My intent is that during deployment, environment variables would be set in my Ubuntu host under /etc/environment, loaded directly in to the process, and then the app would just run for that configuration.

To do this, I have a line at the start of server.js:

if(process.env.NODE_ENV === 'development') {
    logger.info("Loading dotenv for development environment")
    require('dotenv').config();
}

And developers will be instructed to add an environment variable to their system for NODE_ENV.

Now, in my Ubuntu EC2 instance I've setup the /etc/environment to have the environment variables I want (note that NODE_ENV being 'dev' here is just to avoid running dotenv):

PORT=MYPORT
NODE_ENV=dev
APP_SECRET_KEY='MYSECRET'
APP_DATABASE_LOGIN=MYLOGIN
APP_DATABASE_PASSWORD='MYPASS'
APP_DATABASE_HOST=MYHOST
APP_DATABASE_NAME=MYDB
APP_DATABASE_PORT=MYDBPORT

And when I reboot and run printenv they are all populated per the file.

I have setup pm2 to run my application directly from server.js without any additional configuration because as I understand it, process.env is populated automatically from environment variables.

However, when I log the values from process.env, I just get null for everything:

logger.info({
    connectionConfig: {
        host: process.env.APP_DATABASE_HOST
        , login: process.env.APP_DATABASE_LOGIN
        , port: process.env.APP_DATABASE_PORT
        , databaseName: process.env.APP_DATABASE_NAME
    }
});

Is there something wrong with the configuration as-is here?

Note: Per the answer below, I had mistakenly setup my environment variables AFTER starting pm2, and as such pm2 caching was missing them

C Bauer
  • 5,003
  • 4
  • 33
  • 62

3 Answers3

19

The issue is that pm2 caches the environment variables.

You have to do:

# all apps
pm2 restart all --update-env
# specific app
pm2 restart {pid} --update-env

If for some reason that doesn't work, the documented way is:

pm2 reload ecosystem.json --update-env

You can read more in here:

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • 3
    Hi Marcos - thanks for this. Your original question made me investigate and I came to the same conclusion. However, the fix for me was to execute `pm2 restart --update-env all`. Can you put this as the first line of your answer so I can accept? – C Bauer Mar 29 '19 at 15:07
  • 1
    Added that line too. – Marcos Casagrande Mar 29 '19 at 15:10
  • I had mistakenly setup my environment variables AFTER starting pm2 earlier in the process and didn't know about the caching - thanks again! – C Bauer Mar 29 '19 at 15:13
5

I 've gone through the same problem it's because of the integrated terminal in visual studio and visual studio code it seems like they don't have access to any of these variables unless you run the editor in Admin mode.so to solve this problem you just need to start your editor in Amin mode

sishanov
  • 141
  • 2
  • 9
3

Make sure you have this code in app.js file

> const path = require('path');  require('dotenv').config({ path:
> path.join(__dirname, '.env') });
Sujeet Kumar
  • 1,822
  • 22
  • 25