Debug application which is run using pm2

2019-04-03 08:39发布

问题:

Application is run by

pm2 start app.js

or

pm2 start config.json

I want to debug my application locally using node_inspector. I added debug argument

pm2 start --node-args="--debug=7000" app.js

It works fine but if I provide config.json instead of script app.js I don't know how to pass arguments about debug. Next piece of config doesn't work

{
  "apps": [
    {
      "name": "myName",
      "script": "app.js",
      "args": "['--debug=7000']"
      ............
      }
    ]
}

So How to debug application which is run by pm2 and using config?

回答1:

You're almost there, use node_args instead of args:

  • args are your script arguments
  • node_args are arguments that are passed to node executable

    {
      "name": "myName",
      "script": "app.js",
      "node_args": ["--debug=7000"]
    }
    

PM2 json schema.

If someone still has problems with the debug setting after this, in some case you have to disable the cluster mode for the debug setting to be effective.

Also note that you don't need the brackets in the node_args value if you pass all the args as a single string.



回答2:

[pm2 version 3.2.2]

The following would work if you want to attach Vscode with PM2.

In the ecosystem file which is ecosystem.config.js, add the following line under apps.

node_args : ["--inspect"]

Adding this would automatically set two node arguments while invoking the scripts. They are --inspect,--inspect-port=9232.

They can be seen with console.log(process.process.execArgv)

Also, if the number of instances are > 1, then the above argument would keep incrementing this port number for each other node instance under this pm2.

eg., for the second node instance pm2 would pass --inspect,--inspect-port=9233.

In case you explicitly set the inspect-port to a value I see the following as args --inspect,--inspect-port=9200,--inspect-port=9230

And it doesn't seem to use the port you wanted. But I think given the nature of pm2, may be its better to not use a specific port.

Hope this helps.



标签: pm2