Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 984
Kenmore Avatar asked Jun 23 '16 21:06

Kenmore


People also ask

How do I check pm2 process?

To get an overview of the processes currently managed by PM2, you can print out a comprehensive list using the command line utility. The output shows basic information about the running processes like app name and id, the mode ( fork or cluster ), status, uptime, memory footprint, etc.

What does NODE_ENV default to?

We see that it in fact reads NODE_ENV and defaults to 'development' if it isn't set. This variable is exposed to applications via 'app.

What does setting NODE_ENV to production do?

Set NODE_ENV to “production” The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).

Where does process env NODE_ENV come from?

Node. js exposes the current process's environment variables to the script as an object called process. env. From there, the Express web server framework popularized using an environment variable called NODE_ENV as a flag to indicate whether the server should be running in “development” mode vs “production” mode.

Why we need to install PM2 to run NodeJS applications?

We need to install PM2 to run Nodejs applications. PM2 can be installed using NPM , Which installs the latest stable version. For the PM2 to be able to manage any node applications , Then install pm2 should be installed globally PM2 is successfully installed on the system.

How do I start an app in PM2?

Start an app. The simplest way to start, daemonize and monitor your application is by using this command line: $ pm2 start app.js. Or start any other application easily: $ pm2 start bashscript.sh $ pm2 start python-app.py --watch $ pm2 start binary-file -- --port 1520. Some options you can pass to the CLI:

How do I restart a node application from PM2?

With one command, PM2 can ensure that any applications it manages restart when the server reboots. Basically, your node application will start as a service. Run this command to run your application as a service by typing the following: sudo env PATH=$PATH:/usr/local/bin pm2 startup -u safeuser

How to show all environment variables for a running node app?

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. This should be the accepted answer. It explains how to use the commands of pm2 to show all environment variables for a running app. Show activity on this post.


5 Answers

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
like image 117
Kenmore Avatar answered Oct 30 '22 02:10

Kenmore


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.

like image 42
mani Avatar answered Oct 30 '22 02:10

mani


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

like image 30
malix Avatar answered Oct 30 '22 01:10

malix


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.

like image 40
Roman Stetsyshin Avatar answered Oct 30 '22 01:10

Roman Stetsyshin


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.

like image 24
yeiniel Avatar answered Oct 30 '22 01:10

yeiniel