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]);
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.js Commented Sep 17, 2015 at 1:18
  • There is a way to attach. See @Benjamin response below. Commented Sep 17, 2015 at 21:10
  • Yes, I've tried that too. But it only allows you to attach to the main js, not the child process Commented Sep 18, 2015 at 0: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]); Commented Sep 18, 2015 at 13:35
  • That's why. So far I think it's not possible right now Commented Sep 18, 2015 at 13:38

5 Answers 5

34

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

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "autoAttachChildProcesses": true,
  "program": "${workspaceFolder}/index.js"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Upvoting as this is the best solution since this option was added to VSCode in early 2018
This works with cluster not direct implementation of child-process
@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.
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.

7 Comments

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.
I understood you, just make sure to spawn your node process with "--debug" and attach to the standard debug port (5858).
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.
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.
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".
|
3

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

Comments

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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.