Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js console.dir() method

Reference for console.dir() in Node.js: inspecting objects with depth, showHidden, colors and other util.inspect options, and how it differs from console.log().

console.dir() prints an object using util.inspect() and lets you pass the inspection options directly. Unlike console.log(), it never applies format specifiers and it always inspects its first argument, even a string.

The most common use is console.dir(value, { depth: null }), which prints the whole structure instead of stopping at [Object] after two levels.

NODE
console.dir(deepObject, { depth: null });

Try it

Node.js Depth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Other options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
console.dir(obj: any, options?: InspectOptions): void
Name Description
obj The value to print.
options.depth How many levels to expand. Default 2; null or Infinity means no limit.
options.showHidden Also show non-enumerable properties and, for arrays, length. Default false.
options.colors Use ANSI colors. Default: on when the output stream is a color-capable terminal.
options.compact true, false or a number controlling how many inner items fit on one line.
options.breakLength Line width before wrapping.
options.maxArrayLength, options.maxStringLength Limit the items or characters shown.
options.sorted Sort keys alphabetically (or pass a comparator).
options.getters Show getter results (true, 'get' or 'set').

Any other util.inspect() option is accepted as well.

Returns: undefined. Edge cases: circular references print as <ref *1> and [Circular *1].

Usage notes

  • console.log(obj) and console.dir(obj) look alike by default, because both use inspect with depth 2. The difference is that dir accepts options.
  • Custom inspect methods are skipped by default. console.log honors [Symbol.for('nodejs.util.inspect.custom')], but console.dir sets customInspect: false unless you pass customInspect: true.
  • depth: null on huge or cyclic structures can produce enormous output. Combine it with maxArrayLength and maxStringLength.
  • Colors follow the output stream like console.log(): on in a color terminal, off when piped. Pass colors: true or false to force it.
  • To get the string instead of printing, call util.inspect(value, options).
  • Browsers differ: in a browser console.dir shows an interactive tree; in Node it is plain text.
  • Debugging private data: inspect output can include secrets (tokens, headers). Strip them before logging in production.