object{any} Any JavaScript primitive orObject.options{Object}showHidden{boolean} Iftrue,object‘s non-enumerable symbols and properties are included in the formatted result. [WeakMap][] and [WeakSet][] entries are also included as well as user defined prototype properties (excluding method properties). Default:false.depth{number} Specifies the number of times to recurse while formattingobject. This is useful for inspecting large objects. To recurse up to the maximum call stack size passInfinityornull. Default:2.colors{boolean} Iftrue, the output is styled with ANSI color codes. Colors are customizable. See [Customizingutil.inspectcolors][]. Default:false.customInspect{boolean} Iffalse,[util.inspect.custom](depth, opts)functions are not invoked. Default:true.showProxy{boolean} Iftrue,Proxyinspection includes the [targetandhandler][] objects. Default:false.maxArrayLength{integer} Specifies the maximum number ofArray, [TypedArray][], [WeakMap][] and [WeakSet][] elements to include when formatting. Set tonullorInfinityto show all elements. Set to0or negative to show no elements. Default:100.maxStringLength{integer} Specifies the maximum number of characters to include when formatting. Set tonullorInfinityto show all elements. Set to0or negative to show no characters. Default:Infinity.breakLength{integer} The length at which input values are split across multiple lines. Set toInfinityto format the input as a single line (in combination withcompactset totrueor any number >=1). Default:80.compact{boolean|integer} Setting this tofalsecauses each object key to be displayed on a new line. It will also add new lines to text that is longer thanbreakLength. If set to a number, the mostninner elements are united on a single line as long as all properties fit intobreakLength. Short array elements are also grouped together. No text will be reduced below 16 characters, no matter thebreakLengthsize. For more information, see the example below. Default:3.sorted{boolean|Function} If set totrueor a function, all properties of an object, andSetandMapentries are sorted in the resulting string. If set totruethe [default sort][] is used. If set to a function, it is used as a [compare function][].getters{boolean|string} If set totrue, getters are inspected. If set to'get', only getters without a corresponding setter are inspected. If set to'set', only getters with a corresponding setter are inspected. This might cause side effects depending on the getter function. Default:false.
- Returns: {string} The representation of
object.
The util.inspect() method returns a string representation of object that is
intended for debugging. The output of util.inspect may change at any time
and should not be depended upon programmatically. Additional options may be
passed that alter the result.
util.inspect() will use the constructor’s name and/or @@toStringTag to make
an identifiable tag for an inspected value.
class Foo {get [Symbol.toStringTag]() {return 'bar';}}class Bar {}const baz = Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } });util.inspect(new Foo()); // 'Foo [bar] {}'util.inspect(new Bar()); // 'Bar {}'util.inspect(baz); // '[foo] {}'
Circular references point to their anchor by using a reference index:
const { inspect } = require('util');const obj = {};obj.a = [obj];obj.b = {};obj.b.inner = obj.b;obj.b.obj = obj;console.log(inspect(obj));// <ref *1> {// a: [ [Circular *1] ],// b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }// }
The following example inspects all properties of the util object:
const util = require('util');console.log(util.inspect(util, { showHidden: true, depth: null }));
The following example highlights the effect of the compact option:
const util = require('util');const o = {a: [1, 2, [['Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +'eiusmod tempor incididunt ut labore et dolore magna aliqua.','test','foo']], 4],b: new Map([['za', 1], ['zb', 'test']])};console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));// { a:// [ 1,// 2,// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line// 'test',// 'foo' ] ],// 4 ],// b: Map(2) { 'za' => 1, 'zb' => 'test' } }// Setting `compact` to false changes the output to be more reader friendly.console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));// {// a: [// 1,// 2,// [// [// 'Lorem ipsum dolor sit amet, consectetur ' +// 'adipiscing elit, sed do eiusmod tempor ' +// 'incididunt ut labore et dolore magna ' +// 'aliqua.,// 'test',// 'foo'// ]// ],// 4// ],// b: Map(2) {// 'za' => 1,// 'zb' => 'test'// }// }// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a// single line.// Reducing the `breakLength` will split the "Lorem ipsum" text in smaller// chunks.
The showHidden option allows [WeakMap][] and [WeakSet][] entries to be
inspected. If there are more entries than maxArrayLength, there is no
guarantee which entries are displayed. That means retrieving the same
[WeakSet][] entries twice may result in different output. Furthermore, entries
with no remaining strong references may be garbage collected at any time.
const { inspect } = require('util');const obj = { a: 1 };const obj2 = { b: 2 };const weakSet = new WeakSet([obj, obj2]);console.log(inspect(weakSet, { showHidden: true }));// WeakSet { { a: 1 }, { b: 2 } }
The sorted option ensures that an object’s property insertion order does not
impact the result of util.inspect().
const { inspect } = require('util');const assert = require('assert');const o1 = {b: [2, 3, 1],a: '`a` comes before `b`',c: new Set([2, 3, 1])};console.log(inspect(o1, { sorted: true }));// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }console.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }const o2 = {c: new Set([2, 1, 3]),a: '`a` comes before `b`',b: [2, 3, 1]};assert.strict.equal(inspect(o1, { sorted: true }),inspect(o2, { sorted: true }));
util.inspect() is a synchronous method intended for debugging. Its maximum
output length is approximately 128 MB. Inputs that result in longer output will
be truncated.
