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]);
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]);
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.
var runner = spawn('node', ['--debug-brk', scriptPath]);
No luck. Looks like debugger attaches but debugging context is not available. –
Manteau console.log('Hello from child');
console.log('Hello from child2');
console.log('Hello from child3');
First of all debugger skips first line. So you need to have breakpoints after the first line in order to debug. It's not going to stop even if --debug-brk was provided. I'm running Win7 –
Manteau 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.
var runner = spawn('node', ['--debug-brk', scriptPath]);
No luck. Looks like debugger attaches but debugging context is not available. –
Manteau console.log('Hello from child');
console.log('Hello from child2');
console.log('Hello from child3');
First of all debugger skips first line. So you need to have breakpoints after the first line in order to debug. It's not going to stop even if --debug-brk was provided. I'm running Win7 –
Manteau 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:
--debug
command line switch
$ node --debug master.js
master.js
node process using Attach
via debug panelchild.js
processmain
process and attach to child
process using Attach 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)
Just add this to your debugger configuration file
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
}
To attach a debugger to a Process Id. A list of processes will be prompted when you run this config., in which you can select process to which you want to attach debugger.
© 2022 - 2024 — McMap. All rights reserved.
var runner = spawn('node', ['--debug-brk=5858', scriptPath]);
– Manteau