Unist Utility: Find Node After
This package, `unist-util-find-after`, provides a lightweight utility for navigating unist (Universal Syntax Tree) trees by locating a node immediately following a specified node or index within a parent. Currently at stable version 5.0.0, it adheres to semantic versioning with major releases introducing breaking changes, and maintains active development within the unified collective. A key differentiator is its seamless integration with other `unist` and `unified` ecosystem tools, simplifying tree manipulation tasks that could otherwise be implemented manually. It is explicitly an ESM-only package, requiring Node.js 16 or newer, and ships with full TypeScript type definitions to ensure robust development.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/unist-util-find-after/index.js from .../your-file.js not supported.
cause Attempting to use CommonJS `require()` to import `unist-util-find-after` after it transitioned to ESM-only.fixChange your import statement to `import { findAfter } from 'unist-util-find-after';` and ensure your project is configured for ESM. -
TypeError: (0 , unist_util_find_after_1.findAfter) is not a function
cause Incorrect ESM import syntax, often when attempting to import a named export as a default, or when transpilng ESM to CJS incorrectly.fixEnsure you are using named imports: `import { findAfter } from 'unist-util-find-after';`. Do not attempt `import findAfter from 'unist-util-find-after';` as there is no default export. -
Property 'children' does not exist on type 'Node<Data>'. Did you mean 'options'?
cause Attempting to access `children` on a non-parent `Node` type. `parent` in `findAfter` must be a `Parent` node (e.g., `Root`, `Element`).fixEnsure the first argument passed to `findAfter` is a `Parent` node type that has a `children` property, such as a `Root` or `Parent` node from `unist-builder` or a parsed AST. -
Node.js process exits with code 1 due to unsupported API 'exports' map.
cause Running `unist-util-find-after@5.x` on a Node.js version older than 16, which does not fully support the 'exports' field in `package.json`.fixUpgrade your Node.js environment to version 16 or higher. Alternatively, downgrade `unist-util-find-after` to a version compatible with your Node.js environment (e.g., `npm install unist-util-find-after@^4`).
Warnings
- breaking Version 5.0.0 changed the minimum Node.js requirement to Node.js 16 or higher. Older Node.js versions are no longer supported.
- breaking Since version 4.0.0, this package is ESM-only. CommonJS `require()` statements will fail, leading to `ERR_REQUIRE_ESM` errors.
- breaking In version 5.0.0, the `findAfter` function now explicitly yields `undefined` when no matching node is found, instead of `null`. While `null` and `undefined` are often treated similarly, this is a distinct change in return type behavior.
- breaking Version 5.0.0 changed to use `exports` map. If you were previously relying on direct deep imports to private APIs, those paths are no longer guaranteed and may be broken.
- breaking Version 3.0.0 updated its dependency on `unist-util-is`, which could be a breaking change for TypeScript users if the types for `Test` predicates have changed significantly.
Install
-
npm install unist-util-find-after -
yarn add unist-util-find-after -
pnpm add unist-util-find-after
Imports
- findAfter
const findAfter = require('unist-util-find-after')import { findAfter } from 'unist-util-find-after' - findAfter
import { findAfter } from 'https://esm.sh/unist-util-find-after@5' - Node
import type { Node } from 'unist'
Quickstart
import { u } from 'unist-builder';
import { findAfter } from 'unist-util-find-after';
// Example Unist tree structure
const tree = u('tree', [
u('leaf', 'leaf 1'),
u('parent', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
u('leaf', 'leaf 4'),
u('parent', [u('leaf', 'leaf 5')]),
u('leaf', 'leaf 6'),
u('empty'),
u('leaf', 'leaf 7')
]);
// Find the first 'parent' node after the node at index 1 (which is 'parent' with 'leaf 2', 'leaf 3')
console.log('Finding parent after index 1:', findAfter(tree, 1, 'parent'));
// Expected output: { type: 'parent', children: [{ type: 'leaf', value: 'leaf 5' }]}
// Find the first 'leaf' node after 'leaf 4' (which is at index 2)
const leaf4 = tree.children[2];
console.log('Finding leaf after "leaf 4":', findAfter(tree, leaf4, 'leaf'));
// Expected output: { type: 'leaf', value: 'leaf 6' }
// Find a node after 'leaf 7' (the last child) - should be undefined
const leaf7 = tree.children[6];
console.log('Finding node after "leaf 7":', findAfter(tree, leaf7));
// Expected output: undefined