如何在VS Code中调试子Node.JS进程?
以下是我正在尝试调试的代码示例:
var spawn = require('child_process').spawn;
var scriptPath = './child-script.js';
var runner_ = spawn('node', [scriptPath]);
答案 0 :(得分:17)
在启动配置中,如下所示添加"autoAttachChildProcesses": true
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"autoAttachChildProcesses": true,
"program": "${workspaceFolder}/index.js"
}
答案 1 :(得分:3)
您可以轻松地向launch.json添加新的启动配置,允许您使用特定端口附加到正在运行的节点实例:
{
"name": "Attach to Node",
"type": "node",
"address": "localhost",
"port": 5870,
}
请确保使用 - debug 或 - debug-brk 参数派生/生成节点进程。
答案 2 :(得分:3)
答案 3 :(得分:2)
寻找这个npm模块child-process-debug。
我在vscode中创建了2个单独的启动配置:
一个用于主进程,另一个用于子进程
{
"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
}
工作流程如下:
--debug
命令行开关启动主节点进程
$ node --debug master.js
master.js
通过调试面板Attach
节点进程
child.js
流程main
进程分离并使用child
附加到Attach child
进程为了进行调试,您可以使用setTimeout
// master.js
var child = child_process.fork(__dirname + './child.js')
setTimeout(function() {
child.send('...')
}, 5000)
答案 4 :(得分:0)
只需将其添加到调试器配置文件中
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
}
要将调试器附加到进程ID。运行此配置时,将提示进程列表,您可以在其中选择要附加调试器的进程。