How to pass node v8 args and script args to pm2?
Asked Answered
D

4

23

I need to be able to start the app below with pm2 but don't know how to start it with pm2.

node --expose-gc bin/www arg1 arg2 arg3

I know about --node-args but I think is only for --expose-gc.

Deibel answered 29/12, 2014 at 14:46 Comment(0)
D
30

After some digging, I've found out that what I was looking for was the double dash on linux.

The normal code,

node --expose-gc bin/www arg1 arg2 arg3

The same code using pm2

pm2 start bin/www --node-args="--expose-gc" -- arg1 arg2 arg3

All v8 arguments you have to put inside --node-args and all scrips args to be grabbed from process.argv you have to put after the double dash.

I hope that in the future they implement something link --script-args="arg1 arg2 arg3". Would be very nice for those that isn't a linux expert.

Deibel answered 29/12, 2014 at 15:44 Comment(0)
C
21

Another way is to create application declaration json file where you specify args key. Look at documentation on PM2 site.

Example of pm2.json file:

{
  "apps" : [{
    "name"        : "appname",
    "script"      : "app.js",
    "args"        : ["-s", "123"],
    "node_args"   : "--harmony",
    "merge_logs"  : true,
    "cwd"         : "/this/is/a/path/to/start/script",
    "env": {
        "NODE_ENV": "production"
    }
  }]
}

And run it as follows:

$ pm2 start pm2.json
Currey answered 6/9, 2015 at 6:42 Comment(0)
G
2

you can add any custom arguments after -x --,

pm2 start app.js -x -- --prod

and node argument as --node-args="--harmony"

pm2 start app.js --node-args="--harmony"

Both

pm2 start app.js --node-args="--harmony" -x -- --prod

Glorygloryofthesnow answered 19/7, 2016 at 5:45 Comment(1)
For sailsJS, refer my this ans for more specific: https://mcmap.net/q/204539/-how-to-pass-execution-arguments-to-app-using-pm2Glorygloryofthesnow
L
1

I had to expose-gc in my pm2 process.js so I had the do the following:

{
  "apps" : [
    {
      "name"        : "app",
      "script"      : "bin/www",
      "instances"   : 2,
      "exec_mode"   : "cluster",
      "watch"       : false,
      "node_args"   : "--expose-gc",
      "env"         : {"NODE_ENV": "development"}
    }
  ]
}
Lefevre answered 30/5, 2016 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.