Sub topic 11
Debugging in DevTools and the Node.js Inspector
This chapter assumes you can load a script in a web page, run a file with node, and read basic async code. It covers the debugging workflow both runtimes share: pause execution, inspect values, then step through code. The browser and Node.js use the same debugger protocol, so once you learn the browser tools you can use them for Node.js too.
The shared model: breakpoints and the debugger statement
A breakpoint tells the engine to pause before it runs a given line. While the engine is paused you can read local variables, look at the call stack, and evaluate expressions in the current scope. You can set breakpoints in two ways:
- Click a line number in the debugger UI.
- Put a
debugger;statement in your code. It pauses only when a debugger is attached. Otherwise the engine ignores it.
Here is a small bug to hunt:
// total.mjs
function total(items) {
let sum = 0;
for (const item of items) {
debugger; // pauses here on each iteration when a debugger is attached
sum += item.price * item.qty;
}
return sum;
}
const cart = [
{ price: 5, qty: 2 },
{ price: '3', qty: 1 },
];
console.log(total(cart)); // prints "103", not 13When you step through the loop, the second iteration shows the problem. sum is 10 and item.price is the string '3'. '3' * 1 evaluates to the number 3, so the multiplication is fine. To find the real cause, step further or change the data. This kind of pause-and-inspect work is what the debugger does well.
Browser DevTools
Open DevTools with F12, or with Cmd+Option+I on macOS. These are the panels you will use most:
| Panel | Use it for |
|---|---|
| Console | Logging, evaluating expressions, viewing errors with stack traces |
| Sources | Breakpoints, stepping, watch expressions, call stack, scope |
| Network | Checking `fetch` requests, status codes, and timing |
In the Sources panel you have more than plain line breakpoints. A conditional breakpoint pauses only when an expression is true, for example item.qty > 1. A logpoint prints a message without pausing, which spares you from editing the code to add console.log. You can also tick "Pause on exceptions" so the debugger stops at the line that throws.
The Node.js inspector
Node.js has an inspector built in. It speaks the Chrome DevTools Protocol over a WebSocket.
# Start the script and listen for a debugger on 127.0.0.1:9229
node --inspect total.mjs
# Same, but pause before the first line of your code runs
node --inspect-brk total.mjsUse --inspect-brk for short scripts. With plain --inspect, a short script can finish before you attach. To attach, open chrome://inspect in Chrome and click "inspect" under Remote Target. You get the same Sources and Console panels as in the browser. VS Code can attach to the same port too.
For quick terminal sessions, use the built-in CLI debugger:
node inspect total.mjsIts commands are c (continue), n (next line), s (step into), o (step out), and repl. The repl command lets you evaluate variables in the paused scope.
Common mistakes
- Expecting
debugger;to pause with no debugger attached. Without a debugger, the engine skips the statement. - Attaching too late to a short script. Use
--inspect-brk. - Stepping through minified or bundled browser code. Enable source maps so DevTools shows your original files.
- Logging an object and reading it later. In the browser console, an expanded object shows its current state, not the state at log time. Log
structuredClone(obj)orJSON.stringify(obj)to capture a snapshot.
Exercise
You run node --inspect script.js, where script.js finishes in 50 ms, and DevTools never pauses at your breakpoint. Why does this happen, and what should you change?
Show answer
The script exits before you can attach DevTools and set the breakpoint. Run it with node --inspect-brk script.js so Node pauses before the first line, attach through chrome://inspect, set your breakpoints, then resume.
Exercise
How do you pause inside a loop only when item.qty > 1, without editing the source file?
Show answer
In the Sources panel, right-click the line number and add a conditional breakpoint with the expression item.qty > 1. This works in the browser and in a Node.js process attached through DevTools.