babel-walk
raw JSON → 3.0.1 verified Sat Apr 25 auth: no javascript
Lightweight AST traversal library for Babel ASTs, v3.0.1 (released 2023). Provides simple, ancestor, and recursive walk functions that are significantly faster than babel-traverse (~8-16x). Key differentiators: curried API (since v3) for improved TypeScript inference and caching of visitors; no union syntax support; designed for non-transformational walks where performance matters. Release cadence is irregular, with major breaking changes in v2 (rename from babylon-walk, dropped Node <10) and v3 (curried functions).
Common errors
error TypeError: walk.simple is not a function ↓
cause Using CommonJS require with default import expecting ESM default export, or using old non-curried API in v3.
fix
Use const { simple } = require('babel-walk'); then simple(visitors)(node, state);
error Cannot find module 'babel-walk' ↓
cause Package not installed or using old package name babylon-walk.
fix
Run npm install babel-walk and ensure import path is 'babel-walk' not 'babylon-walk'.
error TypeError: Cannot read properties of undefined (reading 'Function') ↓
cause Union syntax like 'FunctionDeclaration|VariableDeclaration' used; not supported.
fix
Define separate visitors for each node type, or use a broader alias like Declaration.
Warnings
breaking v3.0.0: All functions are now curried. Old call signature walk.simple(node, visitors, state) no longer works. ↓
fix Change to walk.simple(visitors)(node, state) or destructure import { simple } and call simple(visitors)(node, state).
breaking v2.0.0: Package renamed from babylon-walk to babel-walk; old package deprecated. ↓
fix Replace require('babylon-walk') with require('babel-walk') or import from 'babel-walk'.
deprecated Union syntax for visitors (e.g., 'NumberLiteral|StringLiteral'(node, state) {}) is not supported. ↓
fix Use separate visitor functions for each node type or use an alias like Expression.
gotcha State argument is required in ancestor walker but defaulted to ancestors array if omitted. In v2, falsy state was replaced by ancestors; in v3, state is always required. ↓
fix Always pass a state object (can be empty object {}) to ancestor walker to avoid confusing defaults.
Install
npm install babel-walk yarn add babel-walk pnpm add babel-walk Imports
- simple wrong
const walk = require('babel-walk'); walk.simple(visitors)(node, state); // v3 curried, use walk.simple(visitors)(node, state) not walk.simple(node, visitors, state)correctimport { simple } from 'babel-walk' - ancestor wrong
const ancestor = require('babel-walk/ancestor'); // wrong path; ancestor is a named export from 'babel-walk'correctimport { ancestor } from 'babel-walk' - recursive wrong
const recursive = require('babel-walk').recursive; // correct in CJS, but note the curried signaturecorrectimport { recursive } from 'babel-walk'
Quickstart
import { parse } from '@babel/parser';
import { simple } from 'babel-walk';
const code = 'const x = 1;';
const ast = parse(code, { sourceType: 'module' });
// Create a walker (curried: visitors first, then node+state)
const walker = simple({
Identifier(node, state) {
console.log(`Found identifier: ${node.name} at line ${node.loc.start.line}`);
},
FunctionDeclaration(node, state) {
state.functions.push(node.id.name);
},
});
const state = { functions: [] };
walker(ast, state);
console.log('Functions:', state.functions);