Unist Tree Inspector
unist-util-inspect is a utility for pretty-printing Abstract Syntax Trees (ASTs) that conform to the unist specification. It provides a human-readable, terser output format specifically tailored for unist nodes, making it easier to debug and understand tree structures compared to verbose JSON representations. The current stable version is 8.1.0, actively maintained with incremental updates. Major releases tend to align with Node.js LTS versions, requiring Node.js 16+ since version 8.0.0 and having transitioned to ESM-only in version 7.0.0. The library aims for a focused utility approach within the broader unified ecosystem, prioritizing clear and concise tree visualization and offering a custom format instead of generic object serialization.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require('unist-util-inspect')` in a CommonJS module after the package became ESM-only.fixChange your import statement to `import { inspect } from 'unist-util-inspect'` and ensure your project is configured for ESM, or use dynamic import `import('unist-util-inspect')`. -
Error: This module requires Node.js 16 or later.
cause Running `unist-util-inspect` on a Node.js version older than 16.fixUpdate your Node.js environment to version 16 or newer. You can use `nvm` to manage Node.js versions easily. -
Property 'inspectColor' does not exist on type 'typeof import("unist-util-inspect")'.cause Using the deprecated `inspectColor` or `inspectNoColor` functions in a TypeScript project, potentially after they have been removed or type definitions have been updated.fixMigrate to using the `color` option with the main `inspect` function: `inspect(tree, { color: true })` for colored output and `inspect(tree, { color: false })` for uncolored output.
Warnings
- breaking Version 8.0.0 and newer require Node.js 16 or later. Running on older Node.js versions will result in an error.
- breaking The package transitioned to ESM-only with an `export` map in v8.0.0, building upon its initial ESM migration in v7.0.0. Direct CommonJS `require()` statements will fail.
- deprecated The `inspectColor` and `inspectNoColor` functions are deprecated. They will be removed in a future major version.
- breaking Version 5.0.0 introduced TypeScript types. While beneficial, this could cause breaking changes for existing TypeScript projects if type definitions conflict or are not handled correctly.
Install
-
npm install unist-util-inspect -
yarn add unist-util-inspect -
pnpm add unist-util-inspect
Imports
- inspect
const inspect = require('unist-util-inspect')import { inspect } from 'unist-util-inspect' - inspectColor
import { inspectColor } from 'unist-util-inspect' - Options
import type { Options } from 'unist-util-inspect'
Quickstart
import { u } from 'unist-builder';
import { inspect } from 'unist-util-inspect';
const complexTree = u('root', [
u('paragraph', [
u('text', 'Hello, world!'),
u('emphasis', [u('text', 'This is emphasized.')])
]),
u('list', { ordered: true }, [
u('listItem', [u('text', 'First item')]),
u('listItem', [u('strong', [u('text', 'Second item with strong text')])])
]),
u('code', { lang: 'js' }, 'console.log("Code example");')
]);
console.log('--- Uncolored Inspection ---');
console.log(inspect(complexTree, { color: false, showPositions: true }));
console.log('\n--- Colored Inspection (default) ---');
console.log(inspect(complexTree));
// Demonstrates basic usage and optional parameters for output control.