Search code examples
node.jsdeploymentpm2

How does pm2 knows what to load?


I am still trying to understand pm2. I have a autodeploy.sh file that contains this:

#! /bin/bash
cd /home/ubuntu/cab_01
sudo -H -u ubuntu bash -c 'git pull origin master'
sudo -H -u ubuntu bash -c '/usr/bin/npm install'
/usr/lib/node_modules/pm2/bin/pm2 startOrGracefulReload ecosystem.json --update-env

How does pm2 knows what environment to use in the ecosystem.json:

 "apps": [
    {
      "name": "cab01",
      "script": "./app.js",
      "env": {
        "environment": "production",
        "NODE_ENV": "production"
      },
      "env_uat":{
        "environment": "uat",
        "NODE_ENV": "uat"
      }
    }
]

Solution

  • By default, pm2 uses NODE_ENV in the env object.

    To override this default behavior and use the NODE_ENV that is in the env_uat object, open your terminal and launch your app using the command below:

    pm2 start ecosystem.config.js --env env_uat

    To learn more about this, checkout this pm2 environment explanation: https://pm2.keymetrics.io/docs/usage/environment/