VS Code, DevTools, and the `debug` Package
Debugging Express
Real debuggers beat console.log every time. Learn to attach.
What you'll learn
- Launch with --inspect-brk
- Use VS Code's debugger
- Use the `debug` package for noisy modules
console.log solves most bugs. For the rest, attach a real
debugger.
Chrome DevTools
node --inspect-brk src/index.js Visit chrome://inspect → click “inspect” → full DevTools: set
breakpoints, watch variables, profile.
--inspect-brk pauses on the first line. --inspect doesn’t.
VS Code
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Express",
"program": "${workspaceFolder}/src/index.js",
"envFile": "${workspaceFolder}/.env",
"runtimeArgs": ["--watch"],
"skipFiles": ["<node_internals>/**"]
}
]
} Press F5. Set breakpoints in the gutter of any file.
Inline debugger;
Drop a debugger; statement anywhere in your code:
app.post("/transfer", (req, res) => {
if (req.body.amount > 10000) {
debugger; // pauses here when running with --inspect
}
// ...
}); The debug Package
Express uses this internally. Enable verbose logging:
DEBUG=express:* node src/index.js Express logs every middleware execution, route match, error path. Invaluable when you don’t understand why a route isn’t matching.
To add it to your own code:
npm install debug import debug from "debug";
const log = debug("myapp:auth");
export function requireAuth(req, res, next) {
log("auth check for %s", req.url);
// ...
} Enable with DEBUG=myapp:* node src/index.js. Disabled = zero
overhead.
Production Debugging
You can’t run --inspect on production. Use:
- Logs — structured, with request IDs, queryable
- Sentry — full stack traces with request context
- Tracing — see the path through your services
- Feature flags — toggle logging without redeploy
Building observability into the app is how you debug production.
Common Express Issues
| Symptom | Likely cause |
|---|---|
| Route matches nothing — 404 | Route declared after a catch-all * |
req.body is undefined | Missing express.json() |
| Cookie won’t stick in dev | secure: true but using HTTP |
| Error middleware doesn’t fire | Not mounted last, or only 3 args |
| Async error crashes the request | Express 4 without express-async-handler |
| Slow under load | Sync work in a handler (fs.readFileSync, bcrypt sync) |