Object Inspection Utility

1.13.4 · active · verified Sun Apr 19

object-inspect is a robust JavaScript utility that provides string representations of arbitrary JavaScript objects, designed for both Node.js and browser environments. Its primary function is to serialize objects into a human-readable string, handling complex data structures like circular references, DOM elements, and various built-in types (e.g., Map, Set, WeakMap, WeakSet, BigInt, WeakRef). The current stable version is 1.13.4. While there isn't a strict release cadence, the project actively incorporates support for new JavaScript features and addresses edge cases, with recent updates adding WeakRef and BigInt support. Key differentiators include its configurable output through options like `depth`, `quoteStyle`, `maxStringLength`, `customInspect`, `indent`, and `numericSeparator`, allowing fine-grained control over the serialization process, making it a versatile alternative or complement to Node.js's built-in `util.inspect` where cross-environment compatibility or customizability is paramount.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `object-inspect`'s ability to handle circular references, control output depth and formatting, and serialize various built-in and primitive types like BigInt, Date, and Map.

const inspect = require('object-inspect');

// Example 1: Circular reference handling
const obj = { a: 1, b: [3, 4] };
obj.c = obj;
console.log('Circular object:', inspect(obj));
// Expected output: Circular object: { a: 1, b: [ 3, 4 ], c: [Circular] }

// Example 2: Deep object with truncation and custom quote style
const deepObj = {
  a: {
    b: {
      c: [1, 2, { d: 'hello world string longer than max length example' }],
    },
  },
  func: function myFunc() {},
  sym: Symbol('test'),
};
console.log('\nDeep object (default):', inspect(deepObj));
console.log('Deep object (depth=2, maxStringLength=10, quoteStyle=\'single\'):', inspect(deepObj, {
  depth: 2,
  maxStringLength: 10,
  quoteStyle: 'single'
}));

// Example 3: BigInt, Date, and Map objects with numeric separators and indent
const mixedData = {
  id: 123456789012345678901234567890n, // BigInt
  date: new Date('2023-10-26T10:00:00Z'),
  map: new Map([['key1', 'val1'], ['key2', 'val2']]),
};
console.log('\nMixed data (numericSeparator=true, indent=\'  \'):', inspect(mixedData, {
  numericSeparator: true,
  indent: '  '
}));

view raw JSON →