Attach VS Code to a running Node.js process
Attach the VS Code debugger to a Node.js process that is already running, using --inspect or a SIGUSR1 signal and an attach launch configuration.
A server is already running, and you cannot restart it with the debug flag. Node.js can open its inspector on request when it receives SIGUSR1, on Linux and macOS.
kill -USR1 12345The process, whose id here is 12345, prints this to its own output:
Debugger listening on ws://127.0.0.1:9229/3b0a8d5e-7c1f-4a62-9f0d-5e2b1c4a6d90
For help, see: https://nodejs.org/en/docs/inspectorThen add an attach configuration to VS Code and start it from the Run panel:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Node.js",
"port": 9229,
"restart": true
}
]
}Breakpoints set in the editor now bind to the running process. With restart enabled, the debugger reconnects after a restart. The attach options are described in the VS Code Node.js debugging guide.
For a process you start yourself, use node --inspect app.js. Pausing a live production process stops its requests, so use this on staging or a copy.