16

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]);
vicneanschi
  • 468
  • 1
  • 4
  • 13
  • As far as I can see, there's no option for the debugger to attach to a certain process in VS Code. However, you could install VS Community 2015 instead. It lets you attach the debugger to a certain process, including node.js –  Sep 17 '15 at 01:18
  • There is a way to attach. See @Benjamin response below. – vicneanschi Sep 17 '15 at 21:10
  • Yes, I've tried that too. But it only allows you to attach to the main js, not the child process –  Sep 18 '15 at 00:52
  • Tried to make it stop on the first line but looks like VS Code can't connect to it. `var runner = spawn('node', ['--debug-brk=5858', scriptPath]);` – vicneanschi Sep 18 '15 at 13:35
  • That's why. So far I think it's not possible right now –  Sep 18 '15 at 13:38
  • Actually I made it work. You need to have a breakpoint on the second line of you _child-schipt.js_ Start main process outsite VS Code. It should stop and wait for the child to finish. Now you can **attach** from VS Code and it should stop at your breakpoint. Let me know if that works for you. – vicneanschi Sep 18 '15 at 13:52
  • Whoa, a clever workaround. I'll definitely try that one –  Sep 18 '15 at 13:56

5 Answers5

34

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

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "autoAttachChildProcesses": true,
  "program": "${workspaceFolder}/index.js"
}
Manoj Suthar
  • 1,415
  • 3
  • 19
  • 41
  • 1
    Upvoting as this is the best solution since this option was added to VSCode in early 2018 – Fran Rodriguez Sep 13 '18 at 11:50
  • This works with cluster not direct implementation of child-process – Gourab Paul May 07 '20 at 12:50
  • @GourabPaul did you find a way to make this work with a direct child process? I'm trying to do this right now and I'm still having trouble with it. I have a forked child process that does not get attached to. – VMS Oct 05 '21 at 22:46
5

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.

vicneanschi
  • 468
  • 1
  • 4
  • 13
Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • 1
    Sorry for not being clear. I'm starting the debug session from VS Code for the parent process. I'd like to to attach magically to the child process. – vicneanschi Sep 17 '15 at 21:08
  • I understood you, just make sure to spawn your node process with "--debug" and attach to the standard debug port (5858). – Benjamin Pasero Sep 18 '15 at 06:17
  • This doesn't work. Once I'm in debug session for the main process VS Code doesn't allow me to launch another debugger for the child. – vicneanschi Sep 18 '15 at 13:18
  • Ok, I understand. Debugging more than one session is currently not supported. To debug the spawned process, I suggest to run the node program outside VS Code and then attach to the spawned process from within VS Code. – Benjamin Pasero Sep 18 '15 at 13:26
  • I've just tried with `var runner = spawn('node', ['--debug-brk', scriptPath]);` No luck. Looks like debugger attaches but debugging context is not available. – vicneanschi Sep 18 '15 at 13:40
  • Here is what I have in _child-script.js_ `console.log('Hello from child');` `console.log('Hello from child2');` `console.log('Hello from child3');` First of all debugger skips first line. So you need to have breakpoints after the first line in order to debug. It's not going to stop even if **--debug-brk** was provided. I'm running Win7 – vicneanschi Sep 18 '15 at 13:48
  • 3
    If you create different launch configs for parent and child (with different names and debug ports), VS Code supports concurrent debug sessions. You can even group them together as a "composed" launch config and start them all with "F5". – Andre Weinand Dec 24 '17 at 14:29
3

Make this change in your launch.json, "autoAttachChildProcesses": true enter image description here

Dan Marshall
  • 644
  • 1
  • 6
  • 9
Ignatius Andrew
  • 8,012
  • 3
  • 54
  • 54
2

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)
7006
  • 21
  • 2
0

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. 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.