Skip to the solution
MediumLow-Level Design7 min read

How can you debug a Node.js application?

383 views

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.

  • debugging
  • tools
  • vscode
  • development

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 10 sections

Read the code

JavaScriptconsole.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;
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 96 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

How do you handle database connection pooling in a Node.js application?

Continue