19

So I just deployed a site with node and pm2 for the first time and I'm going back and doing some optimization and reading best practices, etc.

I read that you can get a lot of benefit by setting NODE_ENV=production.

I found this in the pm2 docs:

[process.json]
"env_production" : {
  "NODE_ENV": "production"
}

...

$ pm2 start process.json --env production

So, I did it but I have no idea if it is working. While trying to figure out how to check it I learned to try:

$ node
> process.env.NODE_ENV
> undefined

So, that's not a good sign.. but, with my limited understanding of how the low level stuff works, I can guess that maybe pm2 launches each app as a separate node process? So maybe I'm not in the right process when I try to check it.

Also, I don't know if I have to make a new ~/.pm2/dump.pm2 file because maybe whenever that is maybe overriding the options I set? (because I used pm2 startup).

How do I check if my pm2 app's NODE_ENV is set?

Kenmore
  • 1,525
  • 3
  • 16
  • 39

8 Answers8

25

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
Kenmore
  • 1,525
  • 3
  • 16
  • 39
  • 1
    You should know that you are a savior @Zuko. You `pm2 kill`ed it, and that was absolutely necessary. – cpz Mar 04 '17 at 13:14
  • How do you set custom env variables in pm2 production? For eg. I want to set process.env.MONGODB_URI. – Flyn Sequeira Sep 09 '18 at 07:17
  • @FlynSequeira You can do that in your config file (this can be a .json file like the pm2.json in my example or in the docs they call it the ecosystem file: ecosystem.config.js). https://pm2.io/doc/en/runtime/guide/ecosystem-file/ . There is probably a way to do this in the CLI as well, just look at the CLI page of the docs. – Kenmore Sep 10 '18 at 17:34
  • `pm2 kill` is that what I was looking for :) – BartusZak Jun 30 '19 at 13:46
11

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.

mani
  • 111
  • 1
  • 3
  • This should be the accepted answer. It explains how to use the commands of `pm2` to show all environment variables for a running app. – lookyhooky Sep 11 '21 at 14:53
6

Start it with npm by adding this to your package.json:

"scripts": {
  "myScript": "NODE_ENV=production pm2 start server.js"
}

Then

npm start myScript

You can do it directly too, but this is easy to manage, automate wth crontab and is in your source control...

malix
  • 3,566
  • 1
  • 31
  • 41
  • Thanks for the "NODE_ENV=production" part at the beggining! It works with "NODE_ENV=production pm2 start npm -- start" directly in the terminal. – Emilio Mar 24 '18 at 23:16
5

Your process.json file is incomplete. Try using something like this:

[process.json]
{
  "name" : "MyApp",
  "script" : "myapp.js",
  "env_production" : {
    "NODE_ENV": "production"
  }
}

Then add logging into your code, preferably somwhere on startup:

console.log("NODE_ENV : ", process.env.NODE_ENV);

Now start the application:

pm2 start process.json --env production

Lastly watch app logs:

pm2 logs MyApp

This should do it.

3

May be at the start of your server script you can print the value of the environment variable and then check the PM2 logs. Use the following code to print your environment variable value:

console.log('process.env.NODE_ENV:', process.env.NODE_ENV);

And then use the following code to see the PM2 logs

pm2 logs app_name

Here app_name is your process name as indicated by the entry in the process.json file.

yeiniel
  • 2,416
  • 15
  • 31
  • OK, I tried that and I still get undefined. So what am I doing wrong? Should I try to wipe my startup script and start over? – Kenmore Jun 23 '16 at 21:58
0

You can set Environment variable for pm2 specifically.

go to /etc/systemd/system/ location. you can see a file named pm2-username.service
file. (eg: pm2-root.service ) you can directly add an Enviorment variable for pm2. for me, it was LD_LIBRARY_PATH . so I added the line as below after the PATH variable.

Environment=PATH=/usr/local/lib......
Environment=LD_LIBRARY_PATH=/opt/oracle/instantclient_21_1

after that, you can restart or start the node application with update-env flag,

pm2 start yourapp --update-env
Rajitha Fernando
  • 1,655
  • 15
  • 14
  • Out of interest, why did you prefer setting LD_LIBRARY_PATH to using `ldconfig` as shown in the [installation instructions](https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html#ic_x64_inst_zip)? Using `ldconfig` can save various hassles. – Christopher Jones Jul 03 '21 at 01:15
0

try pm2 env <app_name/id> also you can find NODE_ENV in pm2 show <app_name/id>

Dipanjan Panja
  • 396
  • 4
  • 12
-4

In your terminal just type:

echo NODE_ENV

it will print current selected environment variable

Shivam Gupta
  • 533
  • 4
  • 9