Skip to solution
mediumLow-Level Design

How can you debug a Node.js application?

383 views
01

Understand the problem

Question presented to candidate: "Beyond scattering console.log statements everywhere, what tools does Node.js actually give you for debugging, and when would you reach for each?"

What a strong answer should cover:

  • console.log is the simplest, most common tool, but Node's console object offers more targeted variants: console.error/console.warn (separate output stream, useful for log-level filtering), console.table (tabular display for arrays of objects), and 📌 console.trace, which prints a message plus a real call stack — genuinely useful for answering "who called this, and through what path" without manually threading that information through.
  • util.inspect (which console.log uses internally for non-string values) gives fine control over how a nested object is printed — { depth: null } in particular removes the default depth limit, which otherwise silently truncates a deeply nested object's printed output.
  • node --inspect (or --inspect-brk, which pauses execution before the first line) starts Node with a debugging protocol server, connectable from Chrome DevTools or VS Code's built-in debugger — enabling real breakpoints, step-through execution, and live variable inspection, categorically more powerful than console statements.
  • The debugger; statement is a genuine breakpoint in code, honored only when a debugger client is actually attached (via --inspect) — otherwise it does nothing at all, a common point of confusion for engineers new to it.
  • For production debugging where attaching an interactive debugger is impractical or unsafe, structured logging (covered fully in its own dedicated question) and heap snapshots/CPU profiles (also covered in their own dedicated questions) are the standard tools — a precise answer distinguishes "debugging a local reproduction" from "diagnosing a live production issue," since the toolset genuinely differs between them.
  • node --trace-warnings/process.on("warning", ...) surface Node's own internal deprecation/warning signals, which are easy to miss silently in normal output otherwise.

Clarifying questions expected:

  • "Is this a local reproduction, or a live production issue?" — the right toolset differs significantly.
  • "Interactive step-through debugging, or understanding a specific already-observed symptom (a leak, a slow request)?" — decides between --inspect and the profiling/logging tools.

Code / implementation expected: Optional — showing console.trace's actual stack output and util.inspect's depth control directly is a concrete, convincing way to demonstrate real familiarity rather than naming tools abstractly.

debuggingtoolsvscodedevelopment
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for Node.js interviews — no prior debugging-tooling experience assumed. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The console output below was actually produced by run

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

console.trace's real call stack and util.inspect's depth control, both actually run
const util = require("util");

function inner() { console.trace("trace point"); }
function outer() { inner(); }
outer();
// Trace: trace point
//     at inner (...)
//     at outer (...)
//     at ...

const obj = { a: 1, nested: { b: [1, 2, 3], c: "xxxxx" } };
console.log(util.inspect(obj, { depth: null, colors: false }));
// { a: 1, nested: { b: [ 1, 2, 3 ], c: 'xxxxx' } }

// A code-level breakpoint — only honored with a debugger actually attached:
function suspiciousFunction(x) {
  debugger; // no-op unless run with: node --inspect script.js
  return x * 2;
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 96 of 152 decoded in the Node.js track. One more won't hurt.

Back to track