{"id":10849,"library":"estree-util-visit","title":"ESTree Utility to Visit Nodes","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/estree-util-visit","tags":["javascript","esast","esast-util","util","utility","recma","estree","typescript"],"install":[{"cmd":"npm install estree-util-visit","lang":"bash","label":"npm"},{"cmd":"yarn add estree-util-visit","lang":"bash","label":"yarn"},{"cmd":"pnpm add estree-util-visit","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package is ESM-only since v2.0.0, so CommonJS `require` is not supported. There is no default export.","wrong":"const visit = require('estree-util-visit').visit","symbol":"visit","correct":"import { visit } from 'estree-util-visit'"},{"note":"Control symbols like CONTINUE, EXIT, and SKIP are named exports, not default. ESM-only.","wrong":"import CONTINUE from 'estree-util-visit'","symbol":"CONTINUE","correct":"import { CONTINUE } from 'estree-util-visit'"},{"note":"TypeScript types are shipped with the package. Import types using `import type`.","symbol":"Visitor","correct":"import type { Visitor } from 'estree-util-visit'"}],"quickstart":{"code":"import { parse } from 'acorn'\nimport { visit, CONTINUE, EXIT, SKIP } from 'estree-util-visit'\n\nconst tree = parse(\n  'export function example() { const x = 1 + \"2\"; console.log(x); if (process.env.DEBUG === \"true\") { process.exit(3) } }',\n  { sourceType: 'module', ecmaVersion: 2020 }\n)\n\nlet literalsFound = 0;\nvisit(tree, function (node) {\n  if (node.type === 'Literal' && 'value' in node) {\n    console.log(`Found literal: ${JSON.stringify(node.value)}`);\n    literalsFound++;\n  }\n  if (literalsFound >= 2) {\n    console.log('Enough literals found, exiting traversal.');\n    return EXIT;\n  }\n  return CONTINUE;\n})\n\n// Example of using enter/leave visitors\nvisit(tree, {\n  enter(node) {\n    // console.log(`Entering: ${node.type}`);\n  },\n  leave(node) {\n    // console.log(`Leaving: ${node.type}`);\n  }\n});","lang":"typescript","description":"Demonstrates parsing an ESTree with Acorn and then traversing it with `estree-util-visit` to find and log literal values, showcasing traversal control with `EXIT`."},"warnings":[{"fix":"Ensure your Node.js environment is version 16 or newer. Update your `package.json` engines field or CI/CD pipelines accordingly.","message":"Version 2.0.0 of `estree-util-visit` now requires Node.js 16 or higher.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Refactor your codebase to use ES module `import` statements (e.g., `import { visit } from 'estree-util-visit'`). Adjust build configurations (e.g., Webpack, Rollup) or Jest setups to handle ESM correctly.","message":"`estree-util-visit` is now an ESM-only package and utilizes the `exports` field in `package.json`. CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update your visitor functions to expect and handle `undefined` instead of `null` for these arguments, e.g., `if (field !== undefined)`.","message":"The `visit` function's callback arguments now pass `undefined` instead of `null` for certain optional values (e.g., `field`, `index`).","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure that your project's `@types/unist` dependency (if any) is updated to a compatible version, or remove it if it's implicitly handled by `estree-util-visit`.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `const { visit } = require('estree-util-visit')` to `import { visit } from 'estree-util-visit'`. Ensure your project is configured for ESM.","cause":"Attempting to use `require()` to import `estree-util-visit`, which is an ESM-only package since version 2.0.0.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/estree-util-visit/index.js from /your/file.js not supported."},{"fix":"Verify 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.","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.","error":"TypeError: (0, _estree_util_visit.visit) is not a function"},{"fix":"Add `CONTINUE` (and any other control symbols you use) to your named import: `import { visit, CONTINUE } from 'estree-util-visit'`.","cause":"The control symbols (CONTINUE, EXIT, SKIP) are named exports and must be explicitly imported.","error":"ReferenceError: CONTINUE is not defined"}],"ecosystem":"npm"}