ESTree Utility to Visit Nodes
estree-util-visit is a core utility within the unified (syntax-tree) ecosystem designed for traversing ESTree (and esast) abstract syntax trees. It enables developers to visit nodes in a depth-first traversal (preorder and/or postorder) for analysis or transformation. The current stable version is 2.0.0. The package distinguishes itself by duck-typing fields for node identification, eliminating the need for a predefined dictionary of node fields, which can simplify usage compared to other walkers. It provides symbolic controls (CONTINUE, EXIT, SKIP) for fine-grained traversal management. Releases are generally tied to ecosystem-wide updates or targeted feature enhancements. Since v2, it is an ESM-only package and requires Node.js 16 or newer.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/estree-util-visit/index.js from /your/file.js not supported.
cause Attempting to use `require()` to import `estree-util-visit`, which is an ESM-only package since version 2.0.0.fixChange your import statement from `const { visit } = require('estree-util-visit')` to `import { visit } from 'estree-util-visit'`. Ensure your project is configured for ESM. -
TypeError: (0, _estree_util_visit.visit) is not a function
cause This error typically occurs when trying to import `visit` as a default export or using an incorrect named import syntax in an ESM environment, or when transpilers misinterpret the import.fixVerify that you are using `import { visit } from 'estree-util-visit'` and not `import visit from 'estree-util-visit'` (there is no default export). Check your Babel/TypeScript configuration if transpiling. -
ReferenceError: CONTINUE is not defined
cause The control symbols (CONTINUE, EXIT, SKIP) are named exports and must be explicitly imported.fixAdd `CONTINUE` (and any other control symbols you use) to your named import: `import { visit, CONTINUE } from 'estree-util-visit'`.
Warnings
- breaking Version 2.0.0 of `estree-util-visit` now requires Node.js 16 or higher.
- breaking `estree-util-visit` is now an ESM-only package and utilizes the `exports` field in `package.json`. CommonJS `require()` is no longer supported.
- breaking The `visit` function's callback arguments now pass `undefined` instead of `null` for certain optional values (e.g., `field`, `index`).
- gotcha The package updated its `@types/unist` dependency. If your project also directly depends on `@types/unist`, you might encounter type conflicts or unexpected type behavior if your versions are incompatible.
Install
-
npm install estree-util-visit -
yarn add estree-util-visit -
pnpm add estree-util-visit
Imports
- visit
const visit = require('estree-util-visit').visitimport { visit } from 'estree-util-visit' - CONTINUE
import CONTINUE from 'estree-util-visit'
import { CONTINUE } from 'estree-util-visit' - Visitor
import type { Visitor } from 'estree-util-visit'
Quickstart
import { parse } from 'acorn'
import { visit, CONTINUE, EXIT, SKIP } from 'estree-util-visit'
const tree = parse(
'export function example() { const x = 1 + "2"; console.log(x); if (process.env.DEBUG === "true") { process.exit(3) } }',
{ sourceType: 'module', ecmaVersion: 2020 }
)
let literalsFound = 0;
visit(tree, function (node) {
if (node.type === 'Literal' && 'value' in node) {
console.log(`Found literal: ${JSON.stringify(node.value)}`);
literalsFound++;
}
if (literalsFound >= 2) {
console.log('Enough literals found, exiting traversal.');
return EXIT;
}
return CONTINUE;
})
// Example of using enter/leave visitors
visit(tree, {
enter(node) {
// console.log(`Entering: ${node.type}`);
},
leave(node) {
// console.log(`Leaving: ${node.type}`);
}
});