An isomorphic port of the Node.js API require('node:util').inspect().
npm install util-inspect-isomorphicA dependency-free isomorphic port of the Node.js API require("node:util").inspect(). Works on any ES6 JS runtime, as it has no dependency on V8 or Node.js. Can be used in the browser, in React Native, etc.
Install as follows:
``sh`
npm install util-inspect-isomorphic
Use just like the original (see the official Node.js docs for full details):
`js
import { inspect } from "util-inspect-isomorphic";
// Serialise Errors with cause:
console.error(inspect(new Error("Outer", { cause: new Error("Inner") })));
// Inspect deeply-nested objects:
console.log(inspect({ a: { b: { c: { d: {} } } } }), { depth: null });
// Print with colours:
console.log(inspect(["wow", new Date(), true], { colors: true }));
// Insert an underscore between every 3 digits:
console.log(inspect(1000000000, { numericSeparator: true }));
`
Requires ES6 and ESM support (or a bundler that can downlevel them).
This library is a port of the Node.js core code in lib/internal/util/inspect.js (and any dependent files) as it was in main on June 2025, so approximately Node.js v24.1.0. Here are the main compromises that had to be made in the port:
- Proxy types cannot be inspected, so showProxy: true has no effect. Instead of the Proxy itself, the Proxy target will be inspected.[External:
- For external (cross-realm, etc.) types, we cannot print the memory address, so just print
.
- Limited Promise introspection – we can't tell whether it is , , or resolved, so we write instead.
- Type inspection can be fooled, as it doesn't use engine-level introspection.
- Not all types of TypedArray get special handling. Namely Float16Array, BigInt64Array, and BigUint64Array.
- Does not apply special colour-highlighting to stack trace lines that enter node_modules or Node.js core modules.Sources
Here's a file-by-file summary of what was adapted from the Node.js internals or other projects:
- src/index.ts:
- lib/internal/util.js
- src/inspect.ts:
- lib/internal/util/inspect.js
- src/internal-assert.ts:
- lib/internal/assert.js
- src/internal-errors.ts:
- lib/internal/errors.js
- src/internal-util-types.ts:
- lib/internal/util/types.js
- Some code adapted from node-inspect-extracted, as per the code comments.
- src/internal-util.ts:
- lib/internal/util.js
- src/internal-validators.ts:
- lib/internal/validators.js
- src/primordials.ts:
- node-primordials, itself a TypeScript port of lib/internal/per_context/primordials.js
- src/node-util.ts:
- src/node_util.cc
- Some code adapted from node-inspect-extracted, as per the code comments.
See also
I found out about the excellent node-inspect-extracted only after largely finishing this port. There are a few differences between our approaches.
-
node-inspect-extracted:
- is a CommonJS library, written in JavaScript.
- is designed to be easy to keep up to date with upstream, whereas util-inspect-isomorphic is a one-time snapshot.
- has some tests.
- util-inspect-isomorphic:
- is a port to ESM, written in TypeScript.
- is confirmed to work in React Native.
- _I found that I had to change calls such as const { Object } = primordials to primordials.Object to get it to run. I can't be sure whether it was a Hermes runtime issue or a Metro bundling issue._
- is untested. Use at your own risk!Contributing
`sh
Clone the repo
git clone git@github.com:shirakaba/util-inspect-isomorphic.git
cd util-inspect-isomorphicInstall the dev dependencies
npm installBuild the code from TypeScript to JavaScript
npm run buildTest the code (e.g. by making a new script)
node ./scripts/test.js
``