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