Search code examples
javascriptnode.jspm2

Argument passing to PM2


I want to put in production a node service : When I launch my application with my arguments like that : node ./backend -c "uf4m6fhnh" -s "SPNLGZsUoSpQ=" -o "8696". Everything works well.

Now I want to put it in production with PM2 : I have tried the 2 ways to to that (CLI and JSON file) like that :

CLI version :

pm2 start backend.js --node-args="-c uf4lvm6fhnh -s SPNLGZsUoSpQ= -o 8696" --name MyAppName

and also :

pm2 start backend.js --name MyAppName -- "-c uf4lvm6fhnh -s SPNLGZsUoSpQ= -o 8696"

Config file (JSON) :

{
  "apps": [
    {
      "name": "MyAppName ",
      "script": "./backend.js",
      "node_args": [
          "-c",
          "uf4lvm6fhnh",
          "-s",
          "SPNLGZsUoSpQ=",
          "-o",
          "8696"
        ]
    }
  ]
}

and then : pm2 start myConfigJson.json

For each of this possible solution, I have the same error in my pm2 logs : Error: Cannot find module '/home/me/Projects/Project/uf4lvm6fhnh' (Note that the not found module is my passed argument)

Any ideas ?


Solution

  • Use args instead.

    node_args is an alias to interpreter_args which passes arguments to node itself, rather than the script. As a result, your command line ends up calling -c|--check on node itself instead.

    See http://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/#programmatic-api and https://nodejs.org/api/cli.html#cli_c_check