Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]);
like image 600
vicneanschi Avatar asked Sep 16 '15 18:09

vicneanschi


People also ask

Can you debug JavaScript in Visual Studio Code?

The Visual Studio Code editor supports debugging of JavaScript running in Microsoft Edge and Google Chrome. You can read more about debugging browsers works in the Browser Debugging documentation.

How do I enable JavaScript debugging in Visual Studio Code?

Open the extensions view (ctrl+shift+x) and search for @builtin @id:ms-vscode. js-debug. Right click on the JavaScript Debugger extension and select Switch to Pre-Release Version . Reload VS Code.


2 Answers

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

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "autoAttachChildProcesses": true,
  "program": "${workspaceFolder}/index.js"
}
like image 63
Manoj Suthar Avatar answered Sep 27 '22 20:09

Manoj Suthar


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.

like image 40
Benjamin Pasero Avatar answered Sep 27 '22 19:09

Benjamin Pasero