How to debug child Node.JS process in Visual Studio Code?

拜拜、爱过 提交于 2020-01-12 13:56:12

问题


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]);

回答1:


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.




回答2:


In your launch configuration add "autoAttachChildProcesses": true like shown below

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "autoAttachChildProcesses": true,
  "program": "${workspaceFolder}/index.js"
}



回答3:


Make this Changes in your Configuration, autoAttachChildProcess: true




回答4:


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:

  1. Start master node process with --debug command line switch $ node --debug master.js
  2. Attach to master.js node process using Attach via debug panel
  3. Place break point in the child.js process
  4. Quickly detach from main 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)


来源:https://stackoverflow.com/questions/32615822/how-to-debug-child-node-js-process-in-visual-studio-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!