{"id":12304,"library":"unist-util-visit-children","title":"Unist Utility for Direct Child Traversal","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/unist-util-visit-children","tags":["javascript","unist","unist-util","util","utility","visit","children","typescript"],"install":[{"cmd":"npm install unist-util-visit-children","lang":"bash","label":"npm"},{"cmd":"yarn add unist-util-visit-children","lang":"bash","label":"yarn"},{"cmd":"pnpm add unist-util-visit-children","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since v2.0.0 and requires Node.js 16+ since v3.0.0. CommonJS `require` will fail.","wrong":"const visitChildren = require('unist-util-visit-children')","symbol":"visitChildren","correct":"import { visitChildren } from 'unist-util-visit-children'"},{"note":"TypeScript type for the visitor callback function.","symbol":"Visitor","correct":"import type { Visitor } from 'unist-util-visit-children'"},{"note":"TypeScript type for the function returned by `visitChildren`.","symbol":"Visit","correct":"import type { Visit } from 'unist-util-visit-children'"}],"quickstart":{"code":"import u from 'unist-builder';\nimport { visitChildren } from 'unist-util-visit-children';\n\nconst visitorFunction = function (node) {\n  console.log(`Visiting direct child: ${node.type}`);\n  if (node.value) {\n    console.log(`  Value: ${node.value}`);\n  }\n};\n\nconst visit = visitChildren(visitorFunction);\n\nconst tree = u('root', [\n  u('leaf', 'leaf 1'),\n  u('parent-node', [\n    u('child-leaf', 'child 2'),\n    u('child-leaf', 'child 3')\n  ]),\n  u('leaf', 'leaf 4'),\n  u('void-node')\n]);\n\nconsole.log('--- Traversing direct children of the root node ---');\nvisit(tree);\n","lang":"javascript","description":"Demonstrates how to import and use `visitChildren` to iterate over the direct children of a unist tree node, logging each child's type."},"warnings":[{"fix":"Ensure your project is running Node.js 16+ and update your `@types/unist` dependency if you are using TypeScript. Update your import paths if using bundlers that do not automatically resolve `export` maps.","message":"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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Migrate your codebase to use ES Modules (ESM) syntax (`import ... from '...'`) and ensure your Node.js environment or bundler is configured for ESM.","message":"Version 2.0.0 converted the package to be ESM-only. CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For most tree traversal scenarios, consider using `unist-util-visit` (or `unist-util-visit-parents` for parent access) for a more robust and widely applicable solution, unless your use case strictly requires only visiting direct children without recursion.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const { visitChildren } = require('unist-util-visit-children')` to `import { visitChildren } from 'unist-util-visit-children'` and ensure your environment supports ESM.","cause":"Attempting to `require()` an ESM-only package.","error":"ERR_REQUIRE_ESM"},{"fix":"Upgrade Node.js to version 16 or newer. If using a bundler, ensure it's configured to process ES Modules from `node_modules`.","cause":"Your Node.js version is too old to handle ES Modules directly, or your bundler isn't configured for ESM.","error":"SyntaxError: Unexpected token 'export' in <path>/node_modules/unist-util-visit-children/index.js"},{"fix":"Ensure you are using named imports: `import { visitChildren } from 'unist-util-visit-children'`. Do not try to destructure a default export, as there is none.","cause":"Incorrect import of the named export `visitChildren`, or the module was loaded improperly.","error":"TypeError: (0, _unist_util_visit_children.visitChildren) is not a function"}],"ecosystem":"npm"}