Debug NodeJS With VS Code

Hello everyone,

In this blog we will look into how to debug your nodejs server application with vscode.

It is actually quite easy. You just need to setup your node server to start in debug mode, which can be done by passing --inspect flag. for example

node --inspect server.js

By default debug will be started at port 9229, but you can of course change it.

After your server started with debug, you have to setup your vscode’s launch.json file inside .vscode folder. Put following content in launch.json file

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "attach",
            "name": "Attach Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "port": 9229,
            "address": "localhost"
        }
    ]
}

Once this is done, press F5 or click on debug icon in vscode.

Now you can set your breakpoint inside vscode by clicking on line number or you can use debugger keyword to set breakpoint and start debugging.

Thanks for reading…

Leave a Reply