15

I launched my node app by using ecosystem.config.js like this.

pm2 start ecosystem.config.js

And my ecosystem.config.js is here.

module.exports = {
  /**
  * Application configuration section
  * http://pm2.keymetrics.io/docs/usage/application-declaration/
  */
  apps : [
    // First application
    {
      name      : "API",
      script    : "./app/index.js",
      env: {
        COMMON_VARIABLE: "true"
      },
      env_production : {
        NODE_ENV: "production"
      }
    },
  ],

  /**
  * Deployment section
  * http://pm2.keymetrics.io/docs/usage/deployment/
  */
  deploy : {
    production : {
      user : "node",
      host : "212.83.163.1",
      ref  : "origin/master",
      repo : "git@github.com:repo.git",
      path : "/var/www/production",
      "post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env production"
    },
    dev : {
      user : "node",
      host : "212.83.163.1",
      ref  : "origin/master",
      repo : "git@github.com:repo.git",
      path : "/var/www/development",
      "post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env dev",
      env  : {
        NODE_ENV: "dev"
      }
    }
  }
}

Then I tried to change the config file for watching mode on. I read this document so, I added watch: true attribute, then I tried pm2 restart ecosystem.config.js --update-env for applying changed config.

The app is restarted and attribute seems to be changed, because when I try pm2 list the watching is enabled. But my app is not restarted after change my code.

So, I just tried pm2 delete 0, pm2 start ecosystem.config.js then It's working well.

Why does --update-env options not working? What do I do wrong?

hshan
  • 655
  • 2
  • 6
  • 19

4 Answers4

30

The only reliable ways I've found to update a pm2 app config is either pm2 kill to stop the daemon, or pm2 delete <id|name> && pm2 start ecosystem.config.js for an individual app (as @hshan mentioned).

This issue claims it was fixed in 2014, but the comments there, plus the string of other questions/issues I found would seem to indicate otherwise: https://github.com/Unitech/pm2/issues/528

Update: Ongoing discussion here as well: https://github.com/Unitech/pm2/issues/3192

Aaron
  • 416
  • 5
  • 6
  • 3
    After about a half hour of stackoverflow, this did it for me... `pm2 kill` did nothing, `pm2 delete` and then `pm2 start ecosystem.config.js` was the way to go! – Tyler V Sep 29 '20 at 03:34
  • #only this helps me pm2 restart ecosystem.config.js --update-env --env production – James M May 18 '21 at 14:48
  • You are a savior – Narayan Singh Aug 25 '21 at 19:04
  • Hey, for me `pm2 stop all`, then `pkill pm2` and then start it again worked out. However, for some reason, when I restart the service, it still shows me the purple message: `Use --update-env to update environment variables` – Vitomir Jan 19 '23 at 08:35
  • Looks like `pm2 kill` workaround works, but it has to be done forever every time after environment variables were updated for the first time ever. Otherwise it keeps loading old variables when just reloading ecosystem file. – Arctomachine Aug 08 '23 at 15:56
12

pm2 restart <pid> --update-env worked for me as suggested in this answer

Huzaifa Iftikhar
  • 755
  • 1
  • 6
  • 9
2
pm2 restart ecosystem.config.js --env production

I don't need --update-env but I do need to specify the env again on restart

Another option that I found was specifying the var on the cmd line:

MY_VAR=1234 pm2 restart ecosystem.config.js

Interestingly, this keeps it in the previous env, but the cmd line var will overwrite the values you have in your ecosystem.config.js for the same var.

However

MY_VAR=1234 pm2 restart ecosystem.config.js --env production

WILL overwrite the cmd line MY_VAR if you have one defined in your ecosystem for production.

sirclesam
  • 2,109
  • 17
  • 13
1

This update error happend when it tried to write to Z:\ drive

However, there is no Z:\ drive in my Windows OS machine

Below helped fix the issue:

1 . Add user environment variable as below:

PM2_HOME=%USERPROFILE%\.pm2

2 . Then kill pm2 daemon once

pm2 kill

Now pm2 update works fine.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140