unist-util-find-before is a specialized utility within the unified ecosystem designed to locate the first node that appears before a specified node or index within a parent node's children. It's currently at version 4.0.1 and follows the unified collective's release cadence, typically aligning major releases with Node.js LTS versions and dropping support for unmaintained Node.js versions. While the core functionality of iterating and finding a node could be implemented manually, this package provides a robust, well-tested, and type-safe solution that integrates seamlessly with other `unist` utilities, promoting consistency and reducing boilerplate when working with abstract syntax trees (ASTs). Its key differentiators include its small size, strong TypeScript support, and adherence to the unified project's compatibility standards, making it a reliable choice for AST manipulation tasks where precise node location is required.
npm install unist-util-find-beforeVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to import and use `findBefore` to locate nodes before a target node or index, with and without a `test` function, within a sample Unist AST.
Upgrade your Node.js environment to version 16 or higher. For projects requiring older Node.js, pin to `unist-util-find-before@^3`.
Migrate your import statements to use ES modules (`import { findBefore } from 'unist-util-find-before';`). If your project is CommonJS-only, consider `unist-util-find-before@^2` or use dynamic `import()`.Ensure your code explicitly checks for `undefined` when using `findBefore` to handle cases where no matching node is found. For example: `const foundNode = findBefore(...); if (foundNode === undefined) { /* handle not found */ }`.Refer to the `unist-util-is` documentation for valid `Test` types. Incorrect test values will likely lead to no node being found or unexpected behavior without clear error messages from `findBefore` itself.
Change `const { findBefore } = require('unist-util-find-before')` to `import { findBefore } from 'unist-util-find-before'` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).Ensure you are using a named import: `import { findBefore } from 'unist-util-find-before'`. The package does not have a default export.Verify that the `parent` variable being passed to `findBefore` is a valid Unist node object. Debug the AST traversal leading up to the `findBefore` call to ensure the parent node exists and is correctly passed.