Pm2: Error using PM2 with node-config when NODE_APP_INSTANCE === 0

Created on 27 Mar 2016  路  7Comments  路  Source: Unitech/pm2

I had big problems using PM2 today with nginx (reverse proxy):

http://serverfault.com/questions/766280/nginx-proxy-pass-cannot-fetch-assets

This seems to be the issue:

https://github.com/lorenwest/node-config/issues/244

and not just warning, I cannot run this app with pm2 at all... is there a way to make this seamlessly work? Does pm2 need a bugfix or it's just not compatible with config package? I tried creating the default-0.json with {} as described in that thread but it didn't help...

This was a hard one to discover that pm2 is at fault... forever and passenger work in this same case...

thank you

Pending Release

Most helpful comment

I'm having the exact same problem and the default-0.json hack didn't work for me either.

The problem is that both PM2 and node-config use the same environment variable, but for very different purposes, and neither provide a namespace.

In the original thread (#1143) that requested the NODE_APP_INSTANCE feature, it was suggested that the name of the environment variable should be configurable. Can we implement that feature? If the environment variable defaults to NODE_APP_INSTANCE it would be a non-breaking change.

All 7 comments

I'm having the exact same problem and the default-0.json hack didn't work for me either.

The problem is that both PM2 and node-config use the same environment variable, but for very different purposes, and neither provide a namespace.

In the original thread (#1143) that requested the NODE_APP_INSTANCE feature, it was suggested that the name of the environment variable should be configurable. Can we implement that feature? If the environment variable defaults to NODE_APP_INSTANCE it would be a non-breaking change.

+1

Same issue here... #244 is marked as fixed which I think is crap since it smells like a total hack. This bug is sitting open for like 9 months with no motion... Think I am going to write off PM2 and move on. I'll take a look at forever and passenger thanks :-)

Published under PM2 2.5 : npm install -g pm2@latest && pm2 update

Here is what i did to address the issue after reading all of the solutions proposed
1- On your server, make sure you have a "config" folder in your project (/var/www/your website name/config)
2- In config folder, make sure you have the configuration files: default.js, development.js, production.js (or json if you prefer)
3- If you use pm2 version less than 2.5, in config folder, create default-0.js with {} as a content (or default-0.json). For pm2 v2.5+ skip step 3 (@vmarchaud thanks for the advice below)
4- Create pm2.config.js like below in your server (put in root folder for example)
NOTE: Setting instance_var: 'INSTANCE_ID' and NODE_CONFIG_DIR are super important
refer to
https://github.com/lorenwest/node-config/wiki/Configuration-Files
http://pm2.keymetrics.io/docs/usage/environment/#specific-environment-variables
docs

module.exports = {
  apps : [
 {
     name      : 'process-name',
      script    : './app.js',
      cwd   : '/var/www/<your website name>/',
      instance_var: 'INSTANCE_ID',
      env: {
        "PORT": 3000,
            "NODE_ENV": 'development',
           "NODE_CONFIG_DIR": "/var/www/<your website name>/config/"
      },
      env_production : {
        "PORT": 8080,
            "NODE_ENV": 'production',
            "NODE_CONFIG_DIR": "/var/www/<your website name>/config/"
      }
    }
  ]
};

5- $ pm2 start pm2.config.js --env production

You have a way to avoid this problem with pm2 2.5 (see docs) :

You may have problems with node-config with the NODE_APP_INSTANCE name,
so you can rename it with instance_var options :

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

@vmarchaud I am using v2.6.1 and i can confirm that i don't need an empty default-0.js/json file.
instance_var and NODE_CONFIG_DIR settings are important to set correctly. I have updated my previous comment, thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

waygee picture waygee  路  4Comments

chaos-git picture chaos-git  路  3Comments

ghost picture ghost  路  3Comments

jubairsaidi picture jubairsaidi  路  3Comments

webchaz picture webchaz  路  3Comments