How to check if my application pm2 NODE_ENV pm2 is installed?

So, I just deployed a site with node and pm2 for the first time, and I come back and do some optimizations and read best practices, etc.

I read that you can get great benefits by setting NODE_ENV=production .

I found this in 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 works. When trying to figure out how to check this, I learned:

 $ node > process.env.NODE_ENV > undefined 

So this is not a good sign. But, with my limited understanding of how low-level stuff works, I can guess that maybe pm2 runs each application as a separate node process? So maybe I'm not in the right process when I try to verify this.

Also, I don’t know if I should create a new ~ / .pm2 / dump.pm2 file, because, when possible, is it possible to override the parameters that I set? (because I used pm2 startup ).

How to check if my application pm2 NODE_ENV pm2 is installed?

+7
express pm2
source share
4 answers

Edit 2:

To more specifically answer the actual question in the title:

In your script file for my Express app.js application, you can use process.env.NODE_ENV to get the current NODE_ENV value and NODE_ENV it out if you want.

An even better way is to use the PM2 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 appear in the pm2 show appname calls (bottom) or in pm2 monit (bottom left).

Edit 1:

It seems that all that is really needed is that you kill and restart the process in order to change your environment.

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

The following is not allowed:

 pm2 restart pm2.json --env production 

You have to kill the process


Original answer:

So, I got him to work. I think this was due to the fact that the environment was saved in the init script, because after clearing everyone and starting work, it worked.

Here is how I did it.

 $ pm2 stop all && pm2 kill && rm -R ~/.pm2 && sudo rm -R /root/.pm2 $ pm2 startup 

Why you can copy-paste the following command:

 $ sudo su -c "env PATH=$PATH:/usr/bin pm2 startup linux -u myusername --hp /home/myusername" 

And then:

 /srv/http/project $ pm2 start pm2.json --env production 

pm2.json is my configuration file.

Then I checked the logs and of course said production instead of undefined .

So, to summarize, I do not think that something is wrong with me, but I already created the init script and saved the configuration, and for some reason did not use the new environment.

+14
source share

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 an entry to your code, preferably sometime at startup:

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

Now run the application:

 pm2 start process.json --env production 

Keep an eye on application logs:

 pm2 logs MyApp 

That should do it.

+4
source share

Perhaps at the beginning of your script server you can print the value of the environment variable and then check the PM2 logs. Use the following code to print the value of an environment variable:

 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 in the entry in the process.json file.

+3
source share

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

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

Then

 npm start myScript 

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

+1
source share

All Articles