Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run "npm start" in debug configuration for NodeJS app in VS Code

I'm currently trying to debug my NodeJS app through visual studio code. When I run the program through PowerShell I run it using "npm start". When trying to debug the app in Visual Studio Code, I get errors inside of NodeJS internal files. This is what my configuration file looks like (it's the default one provided):

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}\\server.js"
        }
    ]
}

Trying to change "program" to "npm start" doesn't work.

like image 443
Taylor Bishop Avatar asked Dec 18 '17 01:12

Taylor Bishop


People also ask

How do I start node js in debug mode?

Open up Preferences > Settings and in the search box type in “node debug”. Under the Extensions tab there should be one extension titled “Node debug”. From here, click the first box: Debug > Node: Auto Attach and set the drop down to “on”. You're almost ready to go now.


1 Answers

You can run npm scripts with a launch configuration, but your npm script needs to start node in debug mode. Example:

In package.json:

"scripts": {
  "start": "node --nolazy --inspect-brk=9229 server.js
}

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "runtimeExecutable": "npm",
            "runtimeArgs": [ "start" ],
            "port": 9229
        }
    ]
}

or, change your launch config to do exactly whatver npm start is doing.

Docs for using 'npm' in launch config: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools

like image 109
Rob Lourens Avatar answered Sep 30 '22 02:09

Rob Lourens