erwin erwin - 11 days ago 10
Node.js Question

PM2 - cannot start my node app using custom cwd in ecosystem.json

My node app is deployed in /opt/hello/current
but when I start PM2 (pm2 startOrReload ecosystem.json --env production) with the following ecosystem.json , it doesn't seems to take in account the given parameters...

"apps" : [
{
"name": "hello",
"cwd": "/opt/hello/current", // the directory from which your app will be launched
"script": "./hello.js", // script path relative to pm2 start
"args": "",
"watch": false,
"node_args": "",
"merge_logs": true,
"env" : {
"NODE_ENV": "development"
},
"env_staging" : {
"NODE_ENV": "staging"
},
"env_production" : {
"NODE_ENV": "production"
}
}]


It should start the script (./hello.js) relatively from "cwd" (/opt/hello/current) ... am I wrong ?

It's currently starting with path /opt/hello/hello.js

Answer

you need to specify a directory path for cwd, in this case, just add a / at the end like so :

 "apps" : [
    {
      "name": "hello",
      "cwd": "/opt/hello/current/",  // / added here
      "script": "./hello.js",
      "args": "",
      "watch": false,
      "node_args": "",
      "merge_logs": true,
      "env" : {
        "NODE_ENV": "development"
      },
      "env_staging" : {
        "NODE_ENV": "staging"
      },
      "env_production" : {
        "NODE_ENV": "production"
      }
    }]