Node.js: How to attach to a running process and to debug the server with a console?

node.jsConsole

node.js Problem Overview


I use 'forever' to run my application. I want to attach to the running environment to inspect my application. So what can I do?

node.js Solutions


Solution 1 - node.js

From http://nodejs.org/api/debugger.html:

> ### Advanced Usage > The V8 debugger can be enabled and accessed either by starting Node with the --debug command-line flag or by signaling an > existing Node process with SIGUSR1.

Find the PID of your node process and then sending SIGUSR1 should do the trick:

kill -s SIGUSR1 nodejs-pid

Then run node-inspector and browse to the URL it indicates. More in this tutorial.

Solution 2 - node.js

Starting from Node 6.3, node has a built-in debugger that can be triggered (even in a production app) by doing:

kill -USR1 <node-pid>

The node process will spit out something like this:

Debugger listening on ws://127.0.0.1:9229/f3f6f226-7dbc-4009-95fa-d516ba132fbd
For help see https://nodejs.org/en/docs/inspector
  • If you can access the server from a browser, you can use chrome://inspect on http://host.domain:9229.

  • If you cannot connect via a browser (e.g. the server is in a firewalled production cluster), you can activate a REPL to inspect over the command line:

      node inspect -p <node-pid>
    
  • If you can't access the server from a browser, but you can SSH into that server, then setup SSH port forwarding (ssh -nNTL 9229:localhost:9229 <username>@<your_host> -i <keyfile>.pem) and you'll find your script under chrome://inspect after a few seconds.

Prior to this version, node-inspector was a separate tool for debugging Node processes. However, as documented on its own page, it is mostly deprecated as the now-bundled debugger is actively maintained and provides more advanced features. For more information on this change, see this thread.

Solution 3 - node.js

Windows users

/edit 2022

This is what I use currently, configured in settings.json. This will prompt you to select a process and it will send the debug signal to it automatically. Read more below to understand what's happening.

enter image description here

  "launch": {
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Attach to Process (Pick)",
        "type": "node",
        "request": "attach",
        "processId": "${command:PickProcess}",
        "port": 9229,
      }
    ]
  },




Previously

If you are on Windows that doesn't support POSIX signals, you can use this workaround from another cmd.

node -e "process._debugProcess(PID)"




For a detailed guide, or to set up debugging in VSCode, follow these simple steps:

  1. In VSCode, open launch.json configuration or create new by clicking on the wheel
    (this is the debug view CtrlShiftD)

enter image description here

  1. The node will listen on port 9229 by default, so add this configuration:
{
  "type": "node",
  "request": "attach",
  "name": "Attach to 9229",
  "port": 9229
},
  1. Open Task Manager and locate the PID of your node process
    I could identify my by the "build" folder where the index.js is. enter image description here
  2. open another cmd or git-bash and run this command,
    where 21392 is the PID of your process.
node -e "process._debugProcess(21392)"
  1. you should see thisDebugger listening on ws:9229
  2. Start debugging from VSCode Attach to 9229
    enter image description here

Everything should be ready now.

Solution 4 - node.js

For me, running node version 6.9.10 I had to:

kill -USR1 <node-pid>

then

node debug -p <node-pid>

the node inspect -p <node-pid> command failed for this version of node.

Solution 5 - node.js

You can add a REPL to your app. For example, if you add a REPL to listen on localhost port 5001, you start your app as usual and login with telnet: telnet localhost 5001. That will take you to a prompt where you can interact with your app directly.

Alternatively, if you need to your app to "pause" when it reaches a certain state, you need to add "debugger;" lines to areas of your code where you want those breakpoints, then start the app in debug mode.

Hope that helps.

Solution 6 - node.js

Even it's an old yet answered question, there is an easier way, which is passing parameters to node:

forever start -c 'node --debug-brk' main.js

If you don't want to wait for debugger to be attached, replace --debug-brk by --debug

Solution 7 - node.js

To inspect nodejs and debug it, use this command

forever -w -c 'node --inspect=IP:PORT' file.js

  • -c if for a custom command

  • use -w to reload if the file is re-save

  • You can pass ip and port for external inspect

  • port custom is 9229

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCroplioView Question on Stackoverflow
Solution 1 - node.jsBillView Answer on Stackoverflow
Solution 2 - node.jsAndrew MaoView Answer on Stackoverflow
Solution 3 - node.jsQwertyView Answer on Stackoverflow
Solution 4 - node.jszink02View Answer on Stackoverflow
Solution 5 - node.jsdanmactoughView Answer on Stackoverflow
Solution 6 - node.jsMiquelView Answer on Stackoverflow
Solution 7 - node.jsDarckBlezzerView Answer on Stackoverflow