How to pass NODE_ENV=production to pm2?

narad picture narad · May 14, 2018 · Viewed 17.7k times · Source

I have this node app deployed and runs fine with NODE_ENV=production yarn start.

I can demonize the app using

pm2 start npm -- start

but then it defaults to NODE_ENV=development config.

And when I use

pm2 start npm -- start NODE_ENV=production

It still starts with development config.

Also I tried passing the env using a process.yml file

apps:
  - script   : index.js
    watch: true
    instances: 4
    env    :
      NODE_ENV: production

but pm2 start npm -- start process.yml still loads the development configs.

How can I fix this?

Answer

AbhinavD picture AbhinavD · May 15, 2018

The reason you might be facing this is because you would have started pm2 with development once. Now it will use that env until you kill it. Following these steps should help

  1. ./node_modules/.bin/pm2 kill
  2. NODE_ENV=production ./node_modules/.bin/pm2 start server.js

You can also use --update-env. From the official docs

By default we want that PM2 doesn’t change process environment while restarting or reloading so they are immutable. If you want to update them, you must use --update-env :