• object {any} Any JavaScript primitive or Object.
    • options {Object}
      • showHidden {boolean} If true, 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 formatting object. This is useful for inspecting large objects. To recurse up to the maximum call stack size pass Infinity or null. Default: 2.
      • colors {boolean} If true, the output is styled with ANSI color codes. Colors are customizable. See [Customizing util.inspect colors][]. Default: false.
      • customInspect {boolean} If false, [util.inspect.custom](depth, opts) functions are not invoked. Default: true.
      • showProxy {boolean} If true, Proxy inspection includes the [target and handler][] objects. Default: false.
      • maxArrayLength {integer} Specifies the maximum number of Array, [TypedArray][], [WeakMap][] and [WeakSet][] elements to include when formatting. Set to null or Infinity to show all elements. Set to 0 or negative to show no elements. Default: 100.
      • maxStringLength {integer} Specifies the maximum number of characters to include when formatting. Set to null or Infinity to show all elements. Set to 0 or negative to show no characters. Default: Infinity.
      • breakLength {integer} The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with compact set to true or any number >= 1). Default: 80.
      • compact {boolean|integer} Setting this to false causes each object key to be displayed on a new line. It will also add new lines to text that is longer than breakLength. If set to a number, the most n inner elements are united on a single line as long as all properties fit into breakLength. Short array elements are also grouped together. No text will be reduced below 16 characters, no matter the breakLength size. For more information, see the example below. Default: 3.
      • sorted {boolean|Function} If set to true or a function, all properties of an object, and Set and Map entries are sorted in the resulting string. If set to true the [default sort][] is used. If set to a function, it is used as a [compare function][].
      • getters {boolean|string} If set to true, 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.

    1. class Foo {
    2. get [Symbol.toStringTag]() {
    3. return 'bar';
    4. }
    5. }
    6. class Bar {}
    7. const baz = Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } });
    8. util.inspect(new Foo()); // 'Foo [bar] {}'
    9. util.inspect(new Bar()); // 'Bar {}'
    10. util.inspect(baz); // '[foo] {}'

    Circular references point to their anchor by using a reference index:

    1. const { inspect } = require('util');
    2. const obj = {};
    3. obj.a = [obj];
    4. obj.b = {};
    5. obj.b.inner = obj.b;
    6. obj.b.obj = obj;
    7. console.log(inspect(obj));
    8. // <ref *1> {
    9. // a: [ [Circular *1] ],
    10. // b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }
    11. // }

    The following example inspects all properties of the util object:

    1. const util = require('util');
    2. console.log(util.inspect(util, { showHidden: true, depth: null }));

    The following example highlights the effect of the compact option:

    1. const util = require('util');
    2. const o = {
    3. a: [1, 2, [[
    4. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +
    5. 'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
    6. 'test',
    7. 'foo']], 4],
    8. b: new Map([['za', 1], ['zb', 'test']])
    9. };
    10. console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
    11. // { a:
    12. // [ 1,
    13. // 2,
    14. // [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line
    15. // 'test',
    16. // 'foo' ] ],
    17. // 4 ],
    18. // b: Map(2) { 'za' => 1, 'zb' => 'test' } }
    19. // Setting `compact` to false changes the output to be more reader friendly.
    20. console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
    21. // {
    22. // a: [
    23. // 1,
    24. // 2,
    25. // [
    26. // [
    27. // 'Lorem ipsum dolor sit amet, consectetur ' +
    28. // 'adipiscing elit, sed do eiusmod tempor ' +
    29. // 'incididunt ut labore et dolore magna ' +
    30. // 'aliqua.,
    31. // 'test',
    32. // 'foo'
    33. // ]
    34. // ],
    35. // 4
    36. // ],
    37. // b: Map(2) {
    38. // 'za' => 1,
    39. // 'zb' => 'test'
    40. // }
    41. // }
    42. // Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a
    43. // single line.
    44. // Reducing the `breakLength` will split the "Lorem ipsum" text in smaller
    45. // 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.

    1. const { inspect } = require('util');
    2. const obj = { a: 1 };
    3. const obj2 = { b: 2 };
    4. const weakSet = new WeakSet([obj, obj2]);
    5. console.log(inspect(weakSet, { showHidden: true }));
    6. // WeakSet { { a: 1 }, { b: 2 } }

    The sorted option ensures that an object’s property insertion order does not impact the result of util.inspect().

    1. const { inspect } = require('util');
    2. const assert = require('assert');
    3. const o1 = {
    4. b: [2, 3, 1],
    5. a: '`a` comes before `b`',
    6. c: new Set([2, 3, 1])
    7. };
    8. console.log(inspect(o1, { sorted: true }));
    9. // { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }
    10. console.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));
    11. // { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
    12. const o2 = {
    13. c: new Set([2, 1, 3]),
    14. a: '`a` comes before `b`',
    15. b: [2, 3, 1]
    16. };
    17. assert.strict.equal(
    18. inspect(o1, { sorted: true }),
    19. inspect(o2, { sorted: true })
    20. );

    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.