Chapter 11 of 12
Bugs in Node.js are inevitable. Knowing how to find and fix them efficiently — rather than adding console.logs until you get lucky — makes a huge difference in productivity.
// Basic output
console.log("Basic log:", value);
console.error("Error message:", err); // goes to stderr
console.warn("Warning:", message);
// Structured output
console.table([{ name: "Nelson", role: "engineer" }, { name: "Alice", role: "designer" }]);
// Renders a formatted table in the terminal
// Timing
console.time("database-query");
await db.findAll(users);
console.timeEnd("database-query"); // database-query: 42.3ms
// Deep object inspection
console.dir(complexObject, { depth: null, colors: true });
// Grouping
console.group("User validation");
console.log("Checking email...");
console.log("Checking password...");
console.groupEnd();# Start in inspect mode
node --inspect server.js
# Break at start (useful for startup errors)
node --inspect-brk server.js
# Open chrome://inspect in Chrome
# Click "Open dedicated DevTools for Node"
# Set breakpoints, step through code, inspect variables
# VS Code: add a launch configuration
# .vscode/launch.json:
{
"type": "node",
"request": "launch",
"name": "Debug Server",
"program": "${workspaceFolder}/src/index.js",
"env": { "NODE_ENV": "development" }
}