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

How to debug Node.JS child process in VS Code?
Here is an example of the code I'm trying to debug:

var spawn = require('child_process').spawn;
var scriptPath = './child-script.js';
var runner_ = spawn('node', [scriptPath]);
+4
source share
4 answers

You can easily add a new launch configuration to launch.json, which allows you to connect to a running instance of node with a specific port:

{
        "name": "Attach to Node",
        "type": "node",
        "address": "localhost",
        "port": 5870,
}

Just make sure fork / create a node process with the argument --debug or -debug-brk.

+4
source

In your launch configuration add "autoAttachChildProcesses": trueas below

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

npm child-process-debug.

vscode:

-,

   {
        "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
    }

:

  • node --debug $ node --debug master.js
  • Join the master.jsnode process using Attachthe debug panel
  • Breakpoint of a place in progress child.js
  • Quickly disconnects from the process mainand joins the process childusingAttach child

For debugging Fro, you can delay message passing between processes with setTimeout

// master.js
var child = child_process.fork(__dirname + './child.js')
setTimeout(function() {
    child.send('...')
}, 5000)
+2
source

Make it Changes in your Configuration, autoAttachChildProcess: true enter image description here

0
source

Source: https://habr.com/ru/post/1607704/


All Articles