Full answer
Levels of debugging, from quick to thorough:
- Console output. console.log is fine for small checks. Better tools: console.table(array) for rows, console.dir(obj, { depth: null }) for deep objects, console.count(label) for how often code runs, console.group for nesting, console.trace() for the call stack, console.error for stderr, and console.time/timeEnd for rough timings.
- The debugger statement. When a debugger is attached, execution pauses at the line debugger;. Without a debugger it does nothing.
- The inspector. node --inspect app.js opens a WebSocket (port 9229 by default) and prints a URL. Open chrome://inspect in Chrome and click inspect, or use the VS Code debugger (launch or attach). node --inspect-brk waits at the first line, which is needed for short scripts. You can set breakpoints, conditional breakpoints and logpoints, step over, into and out, watch expressions, inspect scopes and the async call stack, and take profiles and heap snapshots.
- Environment helpers. NODE_DEBUG=http,net prints internal logs of core modules. The debug package uses DEBUG=app:* for your own namespaced logs.
- The inspector module. new inspector.Session(), session.connect(), and session.post('Runtime.evaluate', ...) use the same protocol from code. It is the basis for taking profiles in a running process.
Security: never expose the inspector port to a network. Anyone who can connect can run any code. Bind to 127.0.0.1 (the default) and tunnel with SSH when debugging a remote process. Sending SIGUSR1 to a Node process also opens the inspector on a running process.
const inspector = require('node:inspector/promises');
const util = require('node:util');
// 1. Console helpers that write to standard output.
console.table([
{ student: 'Asha', marks: 78 },
{ student: 'Ravi', marks: 91 },
]);
console.group('Term 1');
console.log('maths: 78');
console.group('details');
console.log('graded by: Ms Rao');
console.groupEnd();
console.groupEnd();
console.count('login');
console.count('login');
console.dir({ a: { b: { c: { d: 1 } } } }, { depth: null });
console.log(util.inspect({ a: { b: { c: { d: 1 } } } }, { depth: 0 }));
// 2. The debugger statement does nothing without a debugger attached.
debugger;
// 3. Talk to the inspector from inside the program.
async function main() {
console.log('inspector url while closed:', inspector.url());
const session = new inspector.Session();
session.connect();
const { result } = await session.post('Runtime.evaluate', { expression: '6 * 7' });
console.log('Runtime.evaluate result:', result.type, result.value);
session.disconnect();
}
main();console.error and console.trace write to stderr, so the snippet uses only the console methods that write to standard output. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): ┌─────────┬─────────┬───────┐ │ (index) │ student │ marks │ ├─────────┼─────────┼───────┤ │ 0 │ 'Asha' │ 78 │ │ 1 │ 'Ravi' │ 91 │ └─────────┴─────────┴───────┘ Term 1 maths: 78 details graded by: Ms Rao login: 1 login: 2 { a: { b: { c: { d: 1 } } } } { a: [Object] } inspector url while closed: undefined Runtime.evaluate result: number 42
Key points
- console.table, dir, count, group and trace give fast insight.
- --inspect plus DevTools or VS Code gives breakpoints and stepping.
- --inspect-brk pauses on the first line.
- The inspector port gives full code execution, so keep it local.
Common mistakes
- Leaving console.log calls and debugger statements in production code.
- Exposing --inspect=0.0.0.0 on a server.
- Using --inspect for a short script and missing the breakpoint, instead of --inspect-brk.
- Printing a large object with console.log and reading [Object] instead of using depth: null.
Follow-ups
- How do you debug a process that is already running?
- What are logpoints and why are they useful?
- How do source maps help with TypeScript?
Tips
- List the levels from console to inspector. It shows you pick the lightest tool.
- Mention the security risk of the inspector port.
- Know chrome://inspect by name.