Object Inspection Utility
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
-
TypeError: inspect is not a function
cause The `inspect` function was not correctly imported or required into the current module context.fixIn CommonJS modules, use `const inspect = require('object-inspect');`. In ES Modules, use `import inspect from 'object-inspect';`. Verify module type configuration if using TypeScript. -
Object output is truncated or lacks detail (e.g., `[Object]`, `[Array]`, or short strings)
cause The default `depth` or `maxStringLength` options are limiting the output's detail, or you have not explicitly configured them for your needs.fixIncrease the `depth` option (e.g., `inspect(obj, { depth: Infinity })` for full depth) and/or adjust `maxStringLength` (e.g., `inspect(obj, { maxStringLength: null })` for no string truncation) in the `inspect` function call.
Warnings
- gotcha The `customInspect` option, introduced in v1.8.0, defaults to `true`. This means if an object has a `Symbol.for('nodejs.util.inspect.custom')` method or an `inspect` property (for compatibility), it will be invoked. If you intend to bypass custom inspect methods and always get `object-inspect`'s default string representation, set `customInspect: false`.
- gotcha `object-inspect` aims for compatibility with Node.js's `util.inspect` but is a separate, cross-environment utility. Its output may not always perfectly mirror the exact string representation provided by Node.js's built-in `util.inspect`, especially with evolving Node.js versions or highly specific edge cases.
- gotcha When inspecting DOM elements in a browser environment, `object-inspect` provides a concise representation (e.g., `<div id="beep">...</div>`). It does not perform full HTML serialization or provide detailed child node information beyond an ellipsis for brevity.
Install
-
npm install object-inspect -
yarn add object-inspect -
pnpm add object-inspect
Imports
- inspect
import inspect from 'object-inspect';
const inspect = require('object-inspect'); - inspect
const inspect = require('object-inspect');import inspect from 'object-inspect';
Quickstart
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: ' '
}));