Skip to content

How to Debug Node.js in VS Code

DodaTech 1 min read

In this tutorial, you'll learn about How to Debug Node.js in VS Code. We cover key concepts, practical examples, and best practices.

The Problem

You're using console.log() everywhere to debug your Node.js app. VS Code has a built-in debugger that's much more powerful.

Quick Fix

1. Open the Run and Debug panel

Press Ctrl+Shift+D (or click the play icon in the left sidebar).

2. Start debugging

Click Run and Debug then select Node.js.

VS Code starts your app with the debugger attached.

3. Set breakpoints

Click in the gutter (left of line numbers) where you want to pause execution. A red dot appears.

4. Use the debug toolbar

When execution hits a breakpoint:

Button Shortcut Action
Continue F5 Resume execution
Step Over F10 Execute current line, don't enter functions
Step Into F11 Enter the current function call
Step Out Shift+F11 Finish current function and return
Restart Ctrl+Shift+F5 Restart debugging
Stop Shift+F5 Stop debugging

5. Inspect variables

In the VARIABLES section, you can see local, closure, and global variables. Hover over variables in your code to see their current value.

Launch Configuration

Create .vscode/launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}/app.js"
    }
  ]
}

Debugging npm Scripts

Add this to launch.json:

{
  "type": "node",
  "request": "launch",
  "name": "npm start",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run", "start"],
  "skipFiles": ["<node_internals>/**"]
}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro