I was previously using gulp and running gulp to start my application and listeners from the Visual Studio Code debugger but have recently needed to switch to running scripts
I haven't found a solution how to pass --inspect-brk=9229
to node
when you are running external npm run scripts. So here is a solution for the workaround launch configuration.
If you want to debug external npm scripts with parameters or even chain of scripts, you can run them directly from node. For example such scripts as angular-cli run scripts:
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
},
Most of external scrips live in node_modules/bin
folder. You just need to find you script and figure out what js file it runs. For ng
it node_modules/@angular/cli/bin/ng
. So here is final launch configuration:
{
"type": "node",
"request": "launch",
"name": "Launch ng build",
"runtimeExecutable": "node",
"runtimeArgs": [
"--inspect-brk=9229",
"node_modules/@angular/cli/bin/ng"
],
"args": ["build"],
"port": 9229
}
So when you are starting debuging you get next command started:
node --inspect-brk=9229 node_modules/@angular/cli/bin/ng build
Now you are able to put a breakpoint to external scripts.
Configure a new debug target in your .vscode/launch.json:
{
"name": "Attach To npm",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
Config your npm to run the node with --debug-brk option:
"scripts": {
"start": "node app.js",
"debug": "node --debug-brk app.js"
...
Start your app from the shell as:
$npm run debug
The program by default will be waiting in the port 5858 to attach the debugger
So, run the debugger in your visual studio code ("Attach To npm").
Enjoy your debugger :)
NPM scripts and gulp is not really meant for launching your application, but rather for running tasks like compilation. If it is a node application, I would recommend you to configure your launch.json that way without npm. If you have complicated listeners or process managers like PM2, instead start your application manually from the process manager and then use an Attach configuration.
For npm tasks, you can specify a tasks.json with "command": "npm"
and "args": ["run"]
.
It appears that VS Code will support npm scripts and other launch scenarios in the release from October 2016.
Below is an example as it was proposed on GitHub.
packages.json
"scripts": {
"debug": "node --nolazy --debug-brk=5858 myProgram.js"
},
vscode launch config
{
"name": "Launch via NPM",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script", "debug"
],
"port": 5858
}
I tried the solutions given by GutiMac and Jpierson but for some reasons I was not able to make the debugger work with any of those.
An alternative solution which worked fine for me (Ubuntu 16, node 8.9.1, VS 1.8.1) is to use this simple app launcher (to be added in the configuration array of VS launch.json):
{
"type": "node",
"request": "launch",
"name": "Launch Node App",
"program": "${workspaceFolder}/my-app/my-npm-start-script-dir/index.js"
}
It is feasible with npm whithout having to alter your scripts
section in package.json
The trick here is to pass the --inspect-brk=9229
to node.
The command will look like npm run start -- --inspect-brk=9229
Here's the .vscode/launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "${env:NVM_BIN}/npm", //change this to your npm path
"runtimeArgs": [
"run-script",
"start",
"--",
"--inspect-brk=9229"
],
"port": 9229
},
]
}