unist-util-modify-children is a utility within the unist (Universal Syntax Tree) ecosystem designed to create a reusable function for directly modifying the children of a parent node in an AST. The current stable version is 4.0.0, which notably moved to ESM-only and requires Node.js 16+. Releases generally follow semantic versioning, with major versions introducing breaking changes like environment requirements or module system shifts. A key differentiator and strong recommendation from its maintainers is that most users should probably use `unist-util-visit` instead, as `unist-util-modify-children` is intended for very specific, advanced scenarios where direct, in-place manipulation of child arrays is explicitly required and other traversal utilities are insufficient.
npm install unist-util-modify-childrenVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `unist-util-modify-children` with a custom modifier function and apply it to a Unist tree to replace a specific type of parent node with a new structure.
Consider `import { visit } from 'unist-util-visit'` for general tree operations.Upgrade your Node.js environment to version 16 or newer. Use an older package version (e.g., `unist-util-modify-children@3`) if you must support older Node.js.
Update your import statements to use ES modules syntax (e.g., `import { modifyChildren } from 'unist-util-modify-children'`). Ensure your project is configured for ESM.Update your project's `@types/unist` dependency to the latest compatible version or match the version used by `unist-util-modify-children`.
Review your TypeScript configuration and code to ensure compatibility with the official types. Update your `tsconfig.json` if necessary.
Change `const { modifyChildren } = require('unist-util-modify-children')` to `import { modifyChildren } from 'unist-util-modify-children'`. Ensure your project's `package.json` has `"type": "module"` or uses `.mjs` file extensions for ESM files.Verify the import statement is `import { modifyChildren } from 'unist-util-modify-children'` and that your environment supports ESM. Also ensure the package is correctly installed.For TypeScript types, ensure you use `import type { Modifier } from 'unist-util-modify-children'` to explicitly import it as a type, especially in modern TypeScript environments. If using an older TypeScript version or Babel, ensure correct transpilation settings.