Debug application which is run using pm2
Asked Answered
C

3

40

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?

Cavity answered 27/4, 2015 at 15:44 Comment(0)
C
52

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.

Canaille answered 27/4, 2015 at 15:49 Comment(10)
I am new to node and pm2, so what will happen if we add this argument? Will it add debug logs in pm2.log?Skilken
Nope --debug in node_args corresponds to the internal node debugging port.Canaille
If someone still has problems with the debug setting after this, in my case I had 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.Michal
@Michal Thank you so much. Yes indeed I had to switch exec_mode to "fork", otherwise it was using it's own ports. Update: now nodejs uses inspect cmd option. So command up there should be "--inspect=9090" etc.Hopefully
Cluster comment made my day!Funeral
This works for PM2 v2.x but does not work with PM2 v3. See github.com/Unitech/pm2/issues/3227.Guano
instead of debug i tried with inspect it worked for me like this "node_args": ["--inspect"] and opened chrome chrome://inspect/#devices there you will find target you can click on the inspect link, :) One doubt whether it has hot reloading(fast refresh) like in create react app or every time i need to add debugger and restart the serverChristiechristin
For me removing this: "exec_mode" : "cluster_mode" and using --inspect instead of --debug worked. Thanks.Chateau
Note that specifying inspector-port in cluster mode is not working: github.com/Unitech/pm2/issues/5301#issuecomment-1133784160Mortenson
Application started at debug mode at ws://127.0.0.1:9230/beb99cb4-e9f6-4a7e-941f-d70b6391301f But how can I get hit it from postman ?Permalloy
C
9

[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.

Costanzo answered 17/10, 2018 at 22:24 Comment(0)
G
7

Another way to do it would be

env: {
    NODE_OPTIONS: '--inspect'
}

in your ecosystem.config.js file.

Gamy answered 14/6, 2019 at 1:31 Comment(3)
Strangely, this is the only solution, that works for me on a Debian server. The node_args is just ignore, while reported correctly as arguments passed to Node.Veinstone
My pm2 application is running at debug port, but how should I access it. The debug logs shown as ws://127.0.0.1:9230/beb99cb4-e9f6-4a7e-941f-d70b6391301fPermalloy
If you're using a yml file, just ignore the bracersMcbrayer

© 2022 - 2024 — McMap. All rights reserved.