Labs ICT
Pro Login

Debugging Techniques

console.log, debugger, and Node.js inspector.

console.log and Friends

The console module provides several methods for debugging output. console.table displays data in a table format, and console.time measures execution time.

console.log("Basic output");
console.warn("Warning message");
console.error("Error message");

// Structured logging
console.log({ name: "Alice", age: 30 });

// Table format
console.table([{ name: "Alice", age: 30 }, { name: "Bob", age: 25 }]);

// Timing
console.time("loop");
for (let i = 0; i < 1000000; i++) {}
console.timeEnd("loop");

Node.js Inspector

The --inspect flag enables the Node.js debugger. Use --inspect-brk to pause execution on the first line. Then open chrome://inspect in Chrome to attach the debugger.

// Start with debugger
node --inspect app.js

// Break on first line
node --inspect-brk app.js

You can set breakpoints, step through code, and inspect variables in Chrome DevTools.

The debugger Statement

Place the debugger statement anywhere you want execution to pause. It only activates when the inspector is attached.

function processUser(user) {
  debugger;  // Execution pauses here when inspector is attached
  return user.name.toUpperCase();
}

Common Debugging Patterns

Use environment variables to toggle debug output. console.trace prints a call stack, and process.memoryUsage helps identify memory issues.

// Debug with environment variable
if (process.env.DEBUG) {
  console.log("Debug:", variable);
}

// Trace function calls
console.trace("Called from:");

// Check memory usage
console.log(process.memoryUsage());
Try it Yourself →