How can I check if my pm2 app NODE_ENV is getting set?

To answer the actual question in the title:

Within your script, for me my Express app's app.js file, you can use process.env.NODE_ENV to get the current value of NODE_ENV and log that out if you want.

An even better way is to use PM2's Process Metrics module, aka pmx.

yarn add pmx

or

npm install pmx --save

then

const Probe = require('pmx').probe()

Probe.metric({
  name    : 'NODE_ENV',
  value   : function() {
    return process.env.NODE_ENV
  }
})

Now it will show up in calls to pm2 monit (bottom left).


To change your environment:

It is necessary that you kill and restart the process to change your environment.

$ pm2 kill && pm2 start pm2.json --env production

The following isn't good enough:

pm2 restart pm2.json --env production

You can also check your NODE_ENV via running pm2 show <yourServerName>. This will output info about your running server including node env.

In addition, you can check your environment variables via running pm2 env 0. This will show all the environment variables for the running node process.