When starting a new process启动新流程时

PM2 will inject environment in this order when starting a new process :启动新流程时,PM2将按以下顺序注入环境:

module.exports = {
  apps : [
      {
        name: "myapp",
        script: "./app.js",
        watch: true,
        env: {
          "NODE_ENV": "development",
        }
      }
  ]
}

Here you can see that PM2 will override the current environment to add NODE_ENV=development. 在这里,您可以看到PM2将覆盖当前环境以添加NODE_ENV=developmentBut you can also define different environments like this :但您也可以这样定义不同的环境:

module.exports = {
  apps : [
      {
        name: "myapp",
        script: "./app.js",
        watch: true,
        env: {
            "PORT": 3000,
            "NODE_ENV": "development"
        },
        env_production: {
            "PORT": 80,
            "NODE_ENV": "production",
        }
      }
  ]
}

Here the default environment is in the env, but you can decide to use env_production by using pm2 start ecosystem.config.js --env production.这里的默认环境位于env中,但您可以通过使用pm2 start ecosystem.config.js --env production来决定使用env_production

You can define as many environments as you like, just remember that you must pass the name of the environment (after env_) you want to use with --env.您可以定义任意多个环境,只需记住您必须传递要与--env一起使用的环境的名称(在env_之后)。

Specific environment variables特定环境变量

NODE_APP_INSTANCE (PM2 2.5 minimum)

There is the NODE_APP_INSTANCE environment variable that is used to make a difference between process, for example you may want to run a cronjob only on one process, you can just check if process.env.NODE_APP_INSTANCE === '0'. 有一个NODE_APP_INSTANCE环境变量用于区分进程,例如,您可能只想在一个进程上运行cronjob,您只需检查process.env.NODE_APP_INSTANCE === '0'Two processes can never have the same number, its still true after pm2 restart and pm2 scale commands.两个进程不可能有相同的数字,在pm2 restartpm2 scale命令后仍然如此。

You may have problems with node-config with the NODE_APP_INSTANCE name, so you can rename it with instance_var options :使用NODE_APP_INSTANCE名称的node-config可能有问题,因此可以使用instance_var选项重命名它:

module.exports = {
  apps : [
      {
        name: "myapp",
        script: "./app.js",
        watch: true,
        instance_var: 'INSTANCE_ID',
        env: {
            "PORT": 3000,
            "NODE_ENV": "development"
        }
      }
  ]
}

In this case the variable will have the same behavior but will be in process.env.INSTANCE_ID.在这种情况下,变量将具有相同的行为,但将位于process.env.INSTANCE_ID中。

increment_var (PM2 2.5 minimum)

There is an option to ask PM2 to increment a environment variable for each instance launched, for example:有一个选项要求PM2为每个启动的实例增加一个环境变量,例如:

module.exports = {
  apps : [
      {
        name: "myapp",
        script: "./app.js",
        instances: 2,
        exec_mode: "cluster",
        watch: true,
        increment_var : 'PORT',
        env: {
            "PORT": 3000,
            "NODE_ENV": "development"
        }
      }
  ]
}

In this example, if i run pm2 start ecosystem.config.js :在本例中,如果我运行pm2 start ecosystem.config.js

NOTE : It will increment also when scaling using pm2 scale myapp 4, both new instances will have 3002 and 3003 as PORT variable.注意:当使用pm2 scale myapp 4进行缩放时,它也会增加,两个新实例都将30023003作为PORT变量。