Skip to content
jshacks.dev
Debugging

Inspect an object with console.dir

Show nested objects beyond the default depth of two with console.dir and the depth option, instead of the [Object] placeholders console.log prints.

1 min read
Summarize in AI

console.log stops after two levels of nesting and prints [Object] for anything deeper. console.dir lets you choose the depth.

dir.mjsJavaScript
const config = { a: { b: { c: { d: 1 } } } };

console.log(config);
console.dir(config, { depth: null });
Text
{ a: { b: { c: [Object] } } }
{
  a: { b: { c: { d: 1 } } }
}

depth: null means unlimited. A number such as depth: 4 is safer for huge objects. The option list matches util.inspect, so colors: true and showHidden: true also work. See console.dir.

The second output breaks into lines because Node.js only joins up to three nested levels on one line. That is formatting, not a change in the data.

In a browser, console.dir shows an expandable tree, which is more useful for DOM elements than console.log, since log prints them as markup. The depth option applies to Node.js; browsers ignore it.

Beware of circular references: they print as [Circular *1] instead of looping.

Share

More snippets

Debugging

Group related logs with console.group

Indent related console output under a label with console.group and console.groupEnd, so interleaved logs from one request stay readable in Node.js 24.

1 min read
Debugging

Debug a single Vitest test in VS Code

Run one Vitest file under the VS Code debugger with a launch configuration that passes the open file, so breakpoints in tests and source work. Vitest 3.

1 min read
Debugging

Print a stack trace with console.trace

Find out who called a function with console.trace, which prints the message and the current call stack to stderr. Node.js 24 and browsers.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.