Taylor Bishop
Taylor Bishop

Reputation: 519

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.

Upvotes: 6

Views: 20709

Answers (1)

Rob Lourens
Rob Lourens

Reputation: 16109

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

Upvotes: 11

Related Questions