Registry / serialization / unist-util-visit

unist-util-visit

JSON →
library5.1.0jsnpmunverified

unist-util-visit is a fundamental utility for traversing unist (Universal Syntax Tree) compliant abstract syntax trees, which are commonly used in remark, retext, and rehype for processing Markdown, natural language, and HTML. It allows developers to walk a tree and apply a `visitor` function to each node (or specific nodes matching a test) encountered during traversal. The current stable version is 5.1.0, last published 3 months ago (as of January 2026), and the project exhibits an active release cadence with frequent patch/minor updates and occasional major versions that introduce breaking changes, often related to Node.js environment requirements or ESM adoption. A key differentiator is its focus on simple node visiting, contrasting with `unist-util-visit-parents` which provides access to the full parent stack during traversal. This package is ESM-only and requires Node.js 16 or newer.

npm install unist-util-visit
INSTALL
IMPORT
SIG · UNIST-UTIL-VISIT
U
unist-util-visit
serializationjavascriptv5.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

visit
import { visit } from 'unist-util-visit'
const visit = require('unist-util-visit')
Since v3.0.0, this package is ESM-only and has no default export. Direct named import is required.
CONTINUE, EXIT, SKIP
import { CONTINUE, EXIT, SKIP } from 'unist-util-visit'
const { CONTINUE, EXIT, SKIP } = require('unist-util-visit')
These constants are used to control traversal flow from within visitor functions (e.g., stopping traversal or skipping children). ESM-only since v3.0.0.
Visitor
import type { Visitor } from 'unist-util-visit'
This is a TypeScript type for defining the signature of visitor functions. Using `import type` is recommended for type-only imports.
Test
import type { Test } from 'unist-util-visit'
This is a TypeScript type for defining `unist-util-is` compatible test functions used to filter which nodes are visited.

Demonstrates parsing Markdown into a unist tree, then using `visit` to traverse specific node types. It shows how to access node value, index, and parent, and how to use `SKIP` and `EXIT` to control traversal flow.

import { fromMarkdown } from 'mdast-util-from-markdown' import { visit } from 'unist-util-visit' import type { Node, Parent } from 'unist' // For type safety // Example Markdown content const markdownContent = 'Some *emphasis*, **strong**, and `code`.' // Parse the Markdown into a unist (mdast) tree const tree = fromMarkdown(markdownContent) // A type guard to refine the parent type for safer access function isParent(node: Node | undefined): node is Parent { return !!node && 'children' in node; } console.log('--- Visiting all text nodes ---') // Visit 'text' nodes in the tree visit(tree, 'text', function (node, index, parent) { // Ensure 'parent' is a Parent node for type safety if accessing its properties const parentType = isParent(parent) ? parent.type : 'root or no parent' console.log(`[ '${(node as any).value}', '${parentType}', index: ${index} ]`) }) console.log('\n--- Visiting and skipping children ---') // Example of stopping traversal for children of 'emphasis' nodes visit(tree, 'emphasis', function (node, index, parent) { const parentType = isParent(parent) ? parent.type : 'root or no parent' console.log(`Found emphasis (value: ${(node as any).value}), skipping its children. Parent: ${parentType}`) return visit.SKIP // Stop traversing children of this 'emphasis' node }) console.log('\n--- Visiting and exiting all traversal ---') // Example of exiting all traversal when a 'strong' node is found visit(tree, 'strong', function (node, index, parent) { const parentType = isParent(parent) ? parent.type : 'root or no parent' console.log(`Found strong (value: ${(node as any).value}), exiting all traversal. Parent: ${parentType}`) return visit.EXIT // Stop all traversal immediately })
Debug
Known issues
breakingThis package is now ESM-only. CommonJS `require()` is no longer supported for importing `unist-util-visit`.
fix
Migrate your project to use ES modules (`import`/`export`) or use an older version of the library (`unist-util-visit@^2`). Ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions.
affects: >=3.0.0
breakingNode.js 16 or newer is now required. Older Node.js versions are not supported, which may lead to `ERR_REQUIRE_ESM` or other module resolution errors.
fix
Upgrade your Node.js environment to version 16 or higher. The `unified` collective maintains compatibility with maintained Node.js versions.
affects: >=5.0.0
breakingThe package now uses `export` maps, which can affect deep imports or direct file access. Relying on internal paths might break.
fix
Always import directly from `unist-util-visit`. Avoid using private or deep import paths that are not explicitly exported.
affects: >=5.0.0
breakingThe `visitor` function parameters, specifically `index` and `parent`, now pass `undefined` instead of `null` when a value is absent, aligning with modern JavaScript conventions.
fix
Update your `visitor` functions to handle `undefined` checks where `null` was previously expected.
affects: >=5.0.0
breakingSignificant changes to TypeScript types for the `visitor` function occurred in v4.0.0. Type inference for `node` and `parent` is now based on the `tree` and `test` parameters, potentially requiring type assertion or careful handling.
fix
Review your TypeScript code involving `visit` calls, especially the `visitor` function's parameters. You may need to update type definitions or use type guards to properly narrow types.
affects: >=4.0.0
gotchaReplacing a node within a `visitor` function, even if `SKIP` is returned, will still cause its *descendants* to be traversed. This can lead to unexpected behavior if not accounted for.
fix
Be mindful when replacing nodes while attempting to skip children. If this behavior is problematic, consider returning `EXIT` or restructuring your traversal logic.
affects: >=3.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to use `require()` to import `unist-util-visit` in a CommonJS module, but the package is ESM-only.
fix
Change `const visit = require('unist-util-visit')` to `import { visit } from 'unist-util-visit'` and ensure your project uses ES modules (e.g., `"type": "module"` in `package.json`).
TypeError: (0, _unistUtilVisit.visit) is not a function
Incorrectly importing `visit` as a default export (`import visit from '...'`) when it is a named export.
fix
Ensure you use named imports: `import { visit } from 'unist-util-visit'`.
SyntaxError: Cannot use import statement outside a module
Using `import` syntax in a file that Node.js treats as a CommonJS module (e.g., `.js` file without `"type": "module"` in `package.json`, or an older Node.js version).
fix
Update your `package.json` with `"type": "module"`, use `.mjs` file extensions for ESM files, or upgrade Node.js to version 16+.
TS2345: Argument of type 'Node | null | undefined' is not assignable to parameter of type 'Node | undefined'. Type 'null' is not assignable to type 'Node | undefined'.
Your TypeScript code expects `null` for the `parent` or `index` parameters in the visitor function, but `unist-util-visit` v5+ now passes `undefined`.
fix
Update your TypeScript types and logic to explicitly handle `undefined` instead of `null` for these parameters in your `visitor` functions.
Upgrade
Version history
5.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources
unist-util-visit — npm install unist-util-visit · libregistry