How to debug child Node.JS process in VS Code?
Here is the example of the code that I'm trying to debug:
var spawn = require('child_process').spawn;
var scriptPath = './child-script.js';
var runner_ = spawn('node', [scriptPath]);
The Visual Studio Code editor supports debugging of JavaScript running in Microsoft Edge and Google Chrome. You can read more about debugging browsers works in the Browser Debugging documentation.
Open the extensions view (ctrl+shift+x) and search for @builtin @id:ms-vscode. js-debug. Right click on the JavaScript Debugger extension and select Switch to Pre-Release Version . Reload VS Code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With