Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass NODE_ENV=production to pm2?

Tags:

node.js

pm2

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?

like image 889
narad Avatar asked May 14 '18 18:05

narad


People also ask

What is NODE_ENV production?

NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).


1 Answers

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 :

like image 123
AbhinavD Avatar answered Sep 22 '22 23:09

AbhinavD