Debug Node.js with breakpoints (VS Code, Chrome)
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.
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
-
Start Node in inspect mode
--inspectopens the debugger and lets the program run: ideal for a server.--inspect-brkpauses 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_OPTIONSvariable. The debugger listens on127.0.0.1by default: never bind it to0.0.0.0on an exposed server, because it allows running any code.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 -
Attach from Chrome
Open
chrome://inspectin 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.
-
Use the VS Code JavaScript Debug Terminal
This is the fastest method. Open the command palette (
Ctrl+Shift+PorCmd+Shift+P) and run Debug: JavaScript Debug Terminal. In that terminal, anynode,npm run,npxortsxcommand 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. -
Create a launch.json configuration
To start debugging with F5 and always the same settings, create
.vscode/launch.json. Thelaunchconfiguration starts the program, theattachconfiguration connects to a process already started with--inspect, for example in Docker (then publish port 9229 on127.0.0.1only).skipFilesavoids stepping into Node internal code.{ "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>/**"] } ] } -
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-parallelismfor Vitest,--runInBandfor Jest. Filter on one file or one test to stay fast.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 -
Debug TypeScript with source maps
The debugger runs JavaScript: for a breakpoint set in a
.tsfile to work, a source map must link the two. If you compile withtsc, enable"sourceMap": trueintsconfig.jsonand point to the compiled files withoutFilesinlaunch.json. Withtsxor 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.{ "type": "node", "request": "launch", "name": "Launch TS (tsc)", "program": "${workspaceFolder}/src/index.ts", "preLaunchTask": "tsc: build - tsconfig.json", "outFiles": ["${workspaceFolder}/dist/**/*.js"], "sourceMaps": true } -
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
- Using
--inspecton a short script: it ends before the debugger attaches. Use--inspect-brk. - Exposing port 9229 with
--inspect=0.0.0.0or a Docker port published on every interface: anyone who reaches it can run code. - Running tests in parallel: without
--runInBandor--no-file-parallelism, breakpoints never trigger. - Setting the breakpoint in the compiled file instead of the
.ts, or the reverse without source maps. - 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
Stack trace formatter
Formats a stack trace (JS, Python, PHP, Java, .NET, Go), separates your code from libraries and points to the line to open first.
Error finder
Paste an error message or a log: the tool recognises the error and opens the right fix.
JSON validator
Validate, format and minify JSON, with the exact line and column of the error.
More guides
- How to read a stack trace (JS, Python, PHP, Java)
- Debug JavaScript with Chrome DevTools
- Debug PHP with Xdebug 3 (PhpStorm, VS Code)
- Read Docker container logs and debug a crashing container
- Debug an Arduino with the Serial Monitor
- Find the commit that broke your code with git bisect
- How to create a minimal reproducible example