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.
console.dir(deepObject, { depth: null });
Try it
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.
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
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)andconsole.dir(obj)look alike by default, because both useinspectwith depth 2. The difference is thatdiraccepts options.- Custom inspect methods are skipped by default.
console.loghonors[Symbol.for('nodejs.util.inspect.custom')], butconsole.dirsetscustomInspect: falseunless you passcustomInspect: true. depth: nullon huge or cyclic structures can produce enormous output. Combine it withmaxArrayLengthandmaxStringLength.- Colors follow the output stream like
console.log(): on in a color terminal, off when piped. Passcolors: trueorfalseto force it. - To get the string instead of printing, call
util.inspect(value, options). - Browsers differ: in a browser
console.dirshows 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.