console.log() has gotten you this far in the course, and it's a legitimate tool — not just a beginner's crutch. This lesson adds a few more, for the situations where scattering console.log() calls stops being enough.
When Node crashes on an uncaught error, it prints a stack trace — not noise to skip past, but a map of exactly where things went wrong:
TypeError: Cannot read properties of undefined (reading 'name')
at getProductName (/app/products.js:12:20)
at /app/app.js:8:15Read it top to bottom: the error type and message first, then the exact file and line where it happened, then every function call that led there. products.js:12:20 means line 12, character 20 — that's where to look first, not the last line of the trace.
Node ships with a real debugger — no extra package needed. Run a script with --inspect:
node --inspect app.jsThen open Chrome and go to chrome://inspect — it connects to the running Node process, and gives you real breakpoints, the ability to step through code line by line, and inspect variables at each point, in the same DevTools you'd already use to debug a webpage.
The debugger keyword pauses execution right there, if a debugger is attached:
function calculateTotal(items) {
debugger // execution pauses here when run with --inspect
return items.reduce((sum, item) => sum + item.price, 0)
}| Symptom | Likely cause |
|---|---|
req.body is undefined | express.json() (or urlencoded()) isn't registered |
| A request just hangs forever | A middleware function never called next(), or a route handler never called res.end()/res.send() |
| "Cannot find module" | A typo in a require() path, or the package genuinely isn't installed — check node_modules |
| The whole server crashed from one bad request | An error inside a route handler wasn't wrapped in try/catch — see the error-handling lesson |
When a bug isn't obvious, narrow it before reaching for the debugger: comment out code until the error disappears, then add it back piece by piece. It's slower than intuition but it always finds the actual line, and it works on bugs no amount of staring at the code solves.