38

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?

Nawa
  • 2,058
  • 8
  • 26
  • 48

3 Answers3

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.

Veve
  • 6,643
  • 5
  • 39
  • 58
soyuka
  • 8,839
  • 3
  • 39
  • 54
  • I am new to node and pm2, so what will happen if we add this argument? Will it add debug logs in pm2.log? – Deepak Banka Nov 12 '15 at 08:11
  • Nope `--debug` in `node_args` corresponds to the internal [node debugging port](https://nodejs.org/api/debugger.html). – soyuka Nov 12 '15 at 08:45
  • 6
    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. – Herick Aug 17 '16 at 04:25
  • 5
    @Herick 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. – risyasin May 17 '17 at 12:23
  • 1
    Cluster comment made my day! – Kenneth Van den Berghe Nov 29 '17 at 14:24
  • This works for PM2 v2.x but does not work with PM2 v3. See https://github.com/Unitech/pm2/issues/3227. – Rom Jul 19 '18 at 13:21
  • 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 server – Learner Dec 11 '19 at 05:16
  • 1
    For me removing this: "exec_mode" : "cluster_mode" and using --inspect instead of --debug worked. Thanks. – Khatri Dec 11 '20 at 20:13
  • Note that specifying `inspector-port` in `cluster` mode is not working: https://github.com/Unitech/pm2/issues/5301#issuecomment-1133784160 – Aidin May 22 '22 at 01:01
  • 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 ? – Vishnupriya Jun 06 '23 at 09:03
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.

d-_-b
  • 21,536
  • 40
  • 150
  • 256
5

Another way to do it would be

env: {
    NODE_OPTIONS: '--inspect'
}

in your ecosystem.config.js file.

Semyon
  • 527
  • 5
  • 16
  • 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. – Vladius Jan 21 '20 at 16:38
  • 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-d70b6391301f` – Vishnupriya Jun 06 '23 at 08:52