How to debug child Node.JS process in Visual Studio Code?
Look for this npm module child-process-debug.
I created 2 separate launch configurations in vscode:
One for master process, other for child process
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach child",
"type": "node",
"request": "attach",
"port": 5859,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
Workflow as follows:
- Start master node process with
--debug
command line switch$ node --debug master.js
- Attach to
master.js
node process usingAttach
via debug panel - Place break point in the
child.js
process - Quickly detach from
main
process and attach tochild
process usingAttach child
Fro debugging purposes, you may delay message sending between processes using setTimeout
// master.js
var child = child_process.fork(__dirname + './child.js')
setTimeout(function() {
child.send('...')
}, 5000)
Make this change in your launch.json, "autoAttachChildProcesses": true
In your launch configuration add "autoAttachChildProcesses": true
like shown below
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"autoAttachChildProcesses": true,
"program": "${workspaceFolder}/index.js"
}
You can easily add a new launch configuration to launch.json that allows you to attach to a running node instance with a specific port:
{
"name": "Attach to Node",
"type": "node",
"address": "localhost",
"port": 5870,
}
Just make sure you fork/spawn your node process with the --debug or --debug-brk argument.