Sandra
Sandra

Reputation: 315

how to pass custom arguments to app

I'm trying to run node app with pm2 via command line and pass only one argument with it

pm2 start app.js --node-args="41"
pm2 start app.js --env dev --node-args="41"

I tried to run it with developement environment and without it but in both ways it didn't worked. What is the way to do it?

Upvotes: 3

Views: 5535

Answers (2)

Patrick Hund
Patrick Hund

Reputation: 20236

The pm2 command line option --node-args is for passing arguments to the Node.js V8 engine, not to the program running on that engine.

The correct way to pass arguments to your program is this:

pm2 start app.js -- 41

For dev environment:

pm2 start app.js --env dev -- 41

So basically, anything you add after the last two dashes is accessible through process.argv as an array.

Upvotes: 6

SALEH
SALEH

Reputation: 1562

You can define a process file at your project root and pass your env and args like this:

process.json

{
  "apps" : [
    {
      "name"               : "app-prod",
      "script"             : "app.js",
      "env": {
        "NODE_ENV": "production"
      }
    },
    {
      "name"               : "app-dev",
      "script"             : "app.js",
      "args"               : "41",
      "env": {
        "NODE_ENV": "development"
      }
    }
  ]
}

This definition will allow you to pass command line argument (41) that will only be available in your development version. Then, if you want to run development version of your app, execute it as follows

pm2 start process.json --only app-dev

Upvotes: 1

Related Questions