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

5

17

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]);
Manteau answered 16/9, 2015 at 18:16 Comment(7)
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.jsMaine
There is a way to attach. See @Benjamin response below.Manteau
Yes, I've tried that too. But it only allows you to attach to the main js, not the child processMaine
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]);Manteau
That's why. So far I think it's not possible right nowMaine
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.Manteau
Whoa, a clever workaround. I'll definitely try that oneMaine
M
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.

Monserratemonsieur answered 17/9, 2015 at 11:54 Comment(7)
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.Manteau
I understood you, just make sure to spawn your node process with "--debug" and attach to the standard debug port (5858).Monserratemonsieur
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.Manteau
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.Monserratemonsieur
I've just tried with var runner = spawn('node', ['--debug-brk', scriptPath]); No luck. Looks like debugger attaches but debugging context is not available.Manteau
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 Win7Manteau
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".Haematoma
E
34

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

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "autoAttachChildProcesses": true,
  "program": "${workspaceFolder}/index.js"
}
Ethridge answered 26/7, 2018 at 14:27 Comment(3)
Upvoting as this is the best solution since this option was added to VSCode in early 2018Ifc
This works with cluster not direct implementation of child-processImparipinnate
@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.Lifton
M
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.

Monserratemonsieur answered 17/9, 2015 at 11:54 Comment(7)
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.Manteau
I understood you, just make sure to spawn your node process with "--debug" and attach to the standard debug port (5858).Monserratemonsieur
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.Manteau
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.Monserratemonsieur
I've just tried with var runner = spawn('node', ['--debug-brk', scriptPath]); No luck. Looks like debugger attaches but debugging context is not available.Manteau
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 Win7Manteau
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".Haematoma
P
3

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

Poor answered 31/1, 2019 at 11:23 Comment(0)
S
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)
Shoemaker answered 24/10, 2016 at 16:1 Comment(0)
W
1

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.

Woodbridge answered 28/4, 2020 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.