Debugging

Breakpoints, Inspectors, and Better Logs

Debugging

Debug Node with Chrome DevTools or VS Code. Or just better console.log.

3 min read Level 1/5 #nodejs#debugging#devtools
What you'll learn
  • Launch with --inspect-brk
  • Use VS Code's debugger
  • Use console wisely

console.log will solve 80% of your bugs. For the other 20%, you want a real debugger.

Chrome DevTools

node --inspect-brk server.mjs

Visit chrome://inspect, click “inspect” next to your process — full DevTools with breakpoints, watches, profilers, memory snapshots.

--inspect-brk pauses on the first line. --inspect doesn’t pause.

VS Code

.vscode/launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Run server",
      "program": "${workspaceFolder}/src/server.mjs",
      "envFile": "${workspaceFolder}/.env"
    }
  ]
}

Hit F5 to start with the debugger attached.

Conditional Breakpoints

In the gutter of any line, right-click → “Add Conditional Breakpoint”: break only when, say, user.id === 42. Much faster than logging inside a loop.

debugger;

Just write debugger; in your code. The debugger pauses when it hits that line.

function handler(req) {
  if (req.method === "POST") {
    debugger;   // pauses here when running with --inspect
  }
}

Better console

console.dir(obj, { depth: null, colors: true }) for deep objects.

console.table([{a:1,b:2},{a:3,b:4}]) for tabular data.

console.time("fetch") / console.timeEnd("fetch") for quick perf measurements.

console.trace() for a stack trace from any point.

util.inspect

For programmatic deep printing:

import { inspect } from "node:util";
console.log(inspect(huge, { depth: null, colors: true }));

When To Use Which

ToolWhen
console.logSanity checks, fast iteration
debugger; + DevToolsComplex flows, intermittent bugs
VS Code debuggerLong debug sessions, multi-file
console.timeQuick perf measurements
Chrome profilerReal perf investigation
Logging →