[英]How to debug child Node.JS process in Visual Studio Code?
How to debug child Node.JS process in VS Code?如何在 VS Code 中调试子 Node.JS 进程?
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]);
In your launch configuration add "autoAttachChildProcesses": true
like shown below在您的启动配置中添加
"autoAttachChildProcesses": true
如下所示
{
"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:您可以轻松地向 launch.json 添加一个新的启动配置,允许您附加到具有特定端口的正在运行的节点实例:
{
"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.只需确保使用--debug或--debug-brk参数 fork/spawn 节点进程。
Look for this npm module child-process-debug .寻找这个 npm 模块child-process-debug 。
I created 2 separate launch configurations in vscode:我在 vscode 中创建了 2 个单独的启动配置:
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
--debug
命令行开关启动主节点进程$ node --debug master.js
master.js
node process using Attach
via debug panelmaster.js
使用节点处理Attach
经由调试面板child.js
processchild.js
进程中放置断点main
process and attach to child
process using Attach child
Attach child
快速脱离main
进程并附加到child
进程Fro debugging purposes, you may delay message sending between processes using setTimeout
出于调试目的,您可以使用
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.将调试器附加到进程 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.
运行此配置时将提示进程列表,您可以在其中选择要附加调试器的进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.