babel-traverse (legacy, Babel 6 era)

raw JSON →
6.26.0 verified Sat Apr 25 auth: no javascript deprecated

babel-traverse (v6.26.0) is the deprecated Babel 6 package for traversing and manipulating ASTs. It was the core traversal engine behind Babel 6, maintaining tree state and allowing node replacement, removal, and insertion. Key differentiator: provides a visitor pattern for AST mutation via Path objects. Since Babel 7, this package has been replaced by @babel/traverse. v6.26.0 is the final release of the standalone package; it depends on babel-types and babel-generator. It does not ship TypeScript definitions, and is CommonJS-only. The v8.0.0-rc.3 release exists but the babel-traverse name is no longer published under the @babel scope.

error TypeError: traverse is not a function
cause Using named import like `import { traverse } from 'babel-traverse'` when the package has only a default export.
fix
Use import traverse from 'babel-traverse' or const traverse = require('babel-traverse').
error Cannot find module 'babel-traverse'
cause The package is not installed, or you are using a bundler that cannot resolve the name (e.g., Webpack with strict module resolution).
fix
Run npm install babel-traverse@6.26.0 or switch to @babel/traverse for Babel 7+ compatibility.
error Error: Cannot find module 'babel-types' from 'babel-traverse'
cause babel-traverse has a peer dependency on babel-types which is not installed.
fix
Run npm install babel-types@6.26.0.
error TypeError: path.isIdentifier is not a function
cause Using a malformed visitor object or the path object is not a valid NodePath. Common when overriding enter without function.
fix
Ensure visitor callbacks are functions: traverse(ast, { enter(path) { ... } }).
deprecated babel-traverse is deprecated in favor of @babel/traverse (Babel 7+). This package is no longer maintained.
fix Migrate to @babel/traverse: npm install @babel/traverse and update imports to import traverse from '@babel/traverse'.
breaking Babel 8 (v8.0.0-rc.x) introduces strictNullChecks for traverse, which may break code that relied on nullable path properties being unchecked.
fix Upgrade to @babel/traverse and add null checks for path properties (e.g., path.parent, path.container) as they may be null.
gotcha The package does not ship TypeScript declarations; using with TypeScript requires custom type stubs or migration to @babel/traverse.
fix Either write a .d.ts file (e.g., declare module 'babel-traverse' { const traverse: any; export default traverse; }) or switch to @babel/traverse.
gotcha babel-traverse v6 is CommonJS-only and cannot be imported as a named export; default import or require is required.
fix Use `import traverse from 'babel-traverse'` (with esModuleInterop) or `const traverse = require('babel-traverse')`.
gotcha No support for async/await or promise-based traversal; all visitor callbacks are synchronous.
fix Consider using @babel/traverse which supports async visitors via `traverse(ast, { enter: async function(path) { ... } })`.
npm install babel-traverse
yarn add babel-traverse
pnpm add babel-traverse

Parses code with babylon, traverses AST using babel-traverse to rename identifier 'n' to 'x', then generates output with babel-generator.

import * as babylon from 'babylon';
import traverse from 'babel-traverse';
import generate from 'babel-generator';

const code = `function square(n) { return n * n; }`;
const ast = babylon.parse(code);

traverse(ast, {
  enter(path) {
    if (path.isIdentifier({ name: 'n' })) {
      path.node.name = 'x';
    }
  }
});

const output = generate(ast, {}, code);
console.log(output.code); // 'function square(x) { return x * x; }'