Skip to content
  1. LuzineDebug
  2. Guides
  3. Debug Node.js with breakpoints (VS Code, Chrome)
Guide Intermediate ≈ 15 min

Debug Node.js with breakpoints (VS Code, Chrome)

In short

Start Node with --inspect (or --inspect-brk to pause on the first line), then attach a debugger: Chrome through chrome://inspect or VS Code. The easiest option in VS Code is the JavaScript Debug Terminal: any node or npm command started in it stops on your breakpoints.

Be the first to approve this guide

Why this method

console.log shows a value at one moment; a debugger shows the whole program state and lets you step line by line. Node.js ships with the V8 inspector protocol: a single flag is enough to hook Chrome DevTools or VS Code to an Express server, a script, a CLI command or a test suite. This guide covers both approaches, Vitest and Jest tests, and TypeScript, where source maps decide whether your breakpoints work.

What you need

  • Node.js 20 or newer (22 or 24 LTS recommended)
  • VS Code or Google Chrome
  • A project that runs locally

Step by step

  1. Start Node in inspect mode

    --inspect opens the debugger and lets the program run: ideal for a server. --inspect-brk pauses before the first line: essential for a short script that ends before you have time to attach. Node then prints "Debugger listening on ws://127.0.0.1:9229/…".

    To pass the flag to an npm command, use the NODE_OPTIONS variable. The debugger listens on 127.0.0.1 by default: never bind it to 0.0.0.0 on an exposed server, because it allows running any code.

    bash
    node --inspect server.js
    node --inspect-brk scripts/import.js
    node --inspect=127.0.0.1:9230 server.js   # another port
    
    # macOS / Linux
    NODE_OPTIONS="--inspect" npm run dev
    # Windows PowerShell
    $env:NODE_OPTIONS="--inspect"; npm run dev
  2. Attach from Chrome

    Open chrome://inspect in Chrome. Your process appears under Remote Target: click inspect, or Open dedicated DevTools for Node, which reconnects automatically on every restart. If you use another port, add it with Configure.

    You get the usual DevTools: Sources for breakpoints, Console to evaluate expressions in the server context, and Memory to track memory leaks.

  3. Use the VS Code JavaScript Debug Terminal

    This is the fastest method. Open the command palette (Ctrl+Shift+P or Cmd+Shift+P) and run Debug: JavaScript Debug Terminal. In that terminal, any node, npm run, npx or tsx command is attached to the debugger automatically, child processes included.

    Set your breakpoints by clicking in the editor gutter, then run your usual command: execution stops on the first breakpoint reached.

    The debugger; statement in code acts as a breakpoint, but only when a debugger is attached.

  4. Create a launch.json configuration

    To start debugging with F5 and always the same settings, create .vscode/launch.json. The launch configuration starts the program, the attach configuration connects to a process already started with --inspect, for example in Docker (then publish port 9229 on 127.0.0.1 only). skipFiles avoids stepping into Node internal code.

    json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch server",
          "program": "${workspaceFolder}/src/server.js",
          "skipFiles": ["<node_internals>/**"]
        },
        {
          "type": "node",
          "request": "attach",
          "name": "Attach :9229",
          "port": 9229,
          "skipFiles": ["<node_internals>/**"]
        }
      ]
    }
  5. Debug a Vitest or Jest test

    A failing test is the perfect place for a breakpoint: the scenario can be reproduced at will. The simplest way is still to run the test command in the JavaScript Debug Terminal. From the command line, you must force a single process, otherwise the breakpoint lands in a worker the debugger does not see: --no-file-parallelism for Vitest, --runInBand for Jest. Filter on one file or one test to stay fast.

    bash
    npx vitest --inspect-brk --no-file-parallelism tests/cart.test.ts
    
    node --inspect-brk node_modules/jest/bin/jest.js --runInBand tests/cart.test.js
    
    node --inspect-brk --test tests/cart.test.js
  6. Debug TypeScript with source maps

    The debugger runs JavaScript: for a breakpoint set in a .ts file to work, a source map must link the two. If you compile with tsc, enable "sourceMap": true in tsconfig.json and point to the compiled files with outFiles in launch.json. With tsx or the native type stripping of Node 22.18+ and 24 (node src/index.ts), line numbers are preserved and the JavaScript Debug Terminal is enough. A grey, hollow breakpoint means the source map was not found.

    json
    {
      "type": "node",
      "request": "launch",
      "name": "Launch TS (tsc)",
      "program": "${workspaceFolder}/src/index.ts",
      "preLaunchTask": "tsc: build - tsconfig.json",
      "outFiles": ["${workspaceFolder}/dist/**/*.js"],
      "sourceMaps": true
    }
  7. Inspect state while paused

    Once paused, use the Variables pane to read local scope and closures, Watch to follow an expression, and the Debug Console to evaluate code in the current context, for example JSON.stringify(req.body). The Call Stack lets you move up to the callers and see their variables. For an intermittent bug, right-click the gutter and choose a conditional breakpoint or a logpoint.

Mistakes to avoid

  1. Using --inspect on a short script: it ends before the debugger attaches. Use --inspect-brk.
  2. Exposing port 9229 with --inspect=0.0.0.0 or a Docker port published on every interface: anyone who reaches it can run code.
  3. Running tests in parallel: without --runInBand or --no-file-parallelism, breakpoints never trigger.
  4. Setting the breakpoint in the compiled file instead of the .ts, or the reverse without source maps.
  5. Starting two processes in inspect mode on the same port: the second one fails with "address already in use".

Checklist

Did this guide help you?

Approve it in one click: it will be highlighted for other developers.

Frequently asked questions

What is the difference between --inspect and --inspect-brk?

--inspect enables the debugger and lets the program start. --inspect-brk enables it and pauses before the first line, giving you time to attach Chrome or VS Code.

Why is my VS Code breakpoint grey?

VS Code cannot map that file to the running code: missing source map, wrong outFiles, or the file is not loaded yet. Check the TypeScript setup and restart.

How do I debug Node.js inside Docker?

Start Node with --inspect=0.0.0.0:9229 inside the container, publish the port as 127.0.0.1:9229:9229 and use an attach configuration in VS Code. Never publish that port on a reachable server.

Tools to apply this guide

More guides