Unist Utility for Direct Child Traversal
unist-util-visit-children is a focused utility within the unified ecosystem designed to create a reusable function for iterating over only the direct children of a given unist (Universal Syntax Tree) node. The current stable version is 3.0.0. Releases are tied to major Node.js version support, with new major versions often dropping support for unmaintained Node.js versions. While it provides a specific function for direct child visitation, the package's own documentation recommends using `unist-util-visit` for most general tree traversal needs due to its broader capabilities. It's distinct in its narrow scope, avoiding recursive traversal and providing a lightweight, dedicated mechanism for immediate child processing.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` an ESM-only package.fixChange `const { visitChildren } = require('unist-util-visit-children')` to `import { visitChildren } from 'unist-util-visit-children'` and ensure your environment supports ESM. -
SyntaxError: Unexpected token 'export' in <path>/node_modules/unist-util-visit-children/index.js
cause Your Node.js version is too old to handle ES Modules directly, or your bundler isn't configured for ESM.fixUpgrade Node.js to version 16 or newer. If using a bundler, ensure it's configured to process ES Modules from `node_modules`. -
TypeError: (0, _unist_util_visit_children.visitChildren) is not a function
cause Incorrect import of the named export `visitChildren`, or the module was loaded improperly.fixEnsure you are using named imports: `import { visitChildren } from 'unist-util-visit-children'`. Do not try to destructure a default export, as there is none.
Warnings
- breaking Version 3.0.0 changed to use `export` maps, which requires modern Node.js module resolution. It also updated `@types/unist` and requires Node.js 16 or higher.
- breaking Version 2.0.0 converted the package to be ESM-only. CommonJS `require()` is no longer supported.
- gotcha The package itself advises against its general use, stating, 'Probably never! Use `unist-util-visit`.' This utility is specifically for *direct* children, whereas `unist-util-visit` provides full recursive tree traversal.
Install
-
npm install unist-util-visit-children -
yarn add unist-util-visit-children -
pnpm add unist-util-visit-children
Imports
- visitChildren
const visitChildren = require('unist-util-visit-children')import { visitChildren } from 'unist-util-visit-children' - Visitor
import type { Visitor } from 'unist-util-visit-children' - Visit
import type { Visit } from 'unist-util-visit-children'
Quickstart
import u from 'unist-builder';
import { visitChildren } from 'unist-util-visit-children';
const visitorFunction = function (node) {
console.log(`Visiting direct child: ${node.type}`);
if (node.value) {
console.log(` Value: ${node.value}`);
}
};
const visit = visitChildren(visitorFunction);
const tree = u('root', [
u('leaf', 'leaf 1'),
u('parent-node', [
u('child-leaf', 'child 2'),
u('child-leaf', 'child 3')
]),
u('leaf', 'leaf 4'),
u('void-node')
]);
console.log('--- Traversing direct children of the root node ---');
visit(tree);