简体   繁体   English

如何在 Visual Studio Code 中调试子 Node.JS 进程?

[英]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 节点进程。

Make this change in your launch.json, "autoAttachChildProcesses": true在 launch.json 中进行此更改, "autoAttachChildProcesses": true 在此处输入图片说明

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:工作流程如下:

  1. Start master node process with --debug command line switch $ node --debug master.js使用--debug命令行开关启动主节点进程$ node --debug master.js
  2. Attach to master.js node process using Attach via debug panel附加到master.js使用节点处理Attach经由调试面板
  3. Place break point in the child.js processchild.js进程中放置断点
  4. Quickly detach from 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.

相关问题 在Visual Studio Code中运行Node.js,将python脚本作为子进程调用并对其进行调试 - In Visual Studio Code run Node.js, call python script as child-process and debug it Visual Studio Code 中调试控制台中的 Node.js readline - Node.js readline in debug-console in Visual Studio Code 如何在不转译代码的情况下在 Visual Studio Code 中使用 ES6 模块调试 Node.JS? - How to debug Node.JS with ES6 modules in Visual Studio Code without transpiling the code? 如何调试Node.JS子分叉进程? - How to debug Node.JS child forked process? 如何通过附加到正在运行的进程来使用Visual Studio Code在Node上调试Express js服务器代码 - How to debug Express js server code on Node using Visual Studio Code by Attaching to running process 在Visual Studio 2017中调试node.js - Debug node.js in Visual Studio 2017 使用Visual Studio Code调试节点js - Debug node js with Visual Studio Code 如何配置Visual Studio Code以与Node.js一起使用 - How to configure Visual Studio Code to work with Node.js 使用Visual Studio Code调试“必需”的Node.js依赖项 - Use Visual Studio Code to debug Node.js dependency which is “require”d 如何从Visual Studio代码中调试child_process fork过程 - How can I debug a child_process fork process from visual studio code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM