{"id":19371,"library":"babel-traverse","title":"babel-traverse (legacy, Babel 6 era)","description":"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.","status":"deprecated","version":"6.26.0","language":"javascript","source_language":"en","source_url":"https://github.com/babel/babel/tree/master/packages/babel-traverse","tags":["javascript"],"install":[{"cmd":"npm install babel-traverse","lang":"bash","label":"npm"},{"cmd":"yarn add babel-traverse","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-traverse","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for AST node type checking and construction; used internally by traverse.","package":"babel-types","optional":false},{"reason":"May be needed for code generation from traversed AST.","package":"babel-generator","optional":true},{"reason":"Commonly used together for parsing code before traversal.","package":"babylon","optional":true}],"imports":[{"note":"The default export is a function; CommonJS require yields the function directly, no .default needed.","wrong":"const traverse = require('babel-traverse').default;","symbol":"default export (traverse)","correct":"import traverse from 'babel-traverse';"},{"note":"Named export 'traverse' does not exist; the module exports a single default function.","wrong":"import { traverse } from 'babel-traverse';","symbol":"main traverse function (CommonJS)","correct":"const traverse = require('babel-traverse');"},{"note":"Types must be obtained from @babel/traverse or defined locally.","wrong":"import { Visitor } from 'babel-traverse';","symbol":"no named exports","correct":"The module only has a default export; no named exports like 'Visitor' or 'Path' are publicly exported."}],"quickstart":{"code":"import * as babylon from 'babylon';\nimport traverse from 'babel-traverse';\nimport generate from 'babel-generator';\n\nconst code = `function square(n) { return n * n; }`;\nconst ast = babylon.parse(code);\n\ntraverse(ast, {\n  enter(path) {\n    if (path.isIdentifier({ name: 'n' })) {\n      path.node.name = 'x';\n    }\n  }\n});\n\nconst output = generate(ast, {}, code);\nconsole.log(output.code); // 'function square(x) { return x * x; }'","lang":"javascript","description":"Parses code with babylon, traverses AST using babel-traverse to rename identifier 'n' to 'x', then generates output with babel-generator."},"warnings":[{"fix":"Migrate to @babel/traverse: npm install @babel/traverse and update imports to import traverse from '@babel/traverse'.","message":"babel-traverse is deprecated in favor of @babel/traverse (Babel 7+). This package is no longer maintained.","severity":"deprecated","affected_versions":"6.x"},{"fix":"Upgrade to @babel/traverse and add null checks for path properties (e.g., path.parent, path.container) as they may be null.","message":"Babel 8 (v8.0.0-rc.x) introduces strictNullChecks for traverse, which may break code that relied on nullable path properties being unchecked.","severity":"breaking","affected_versions":">=8.0.0-rc.0"},{"fix":"Either write a .d.ts file (e.g., declare module 'babel-traverse' { const traverse: any; export default traverse; }) or switch to @babel/traverse.","message":"The package does not ship TypeScript declarations; using with TypeScript requires custom type stubs or migration to @babel/traverse.","severity":"gotcha","affected_versions":"6.x"},{"fix":"Use `import traverse from 'babel-traverse'` (with esModuleInterop) or `const traverse = require('babel-traverse')`.","message":"babel-traverse v6 is CommonJS-only and cannot be imported as a named export; default import or require is required.","severity":"gotcha","affected_versions":"6.x"},{"fix":"Consider using @babel/traverse which supports async visitors via `traverse(ast, { enter: async function(path) { ... } })`.","message":"No support for async/await or promise-based traversal; all visitor callbacks are synchronous.","severity":"gotcha","affected_versions":"6.x"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `import traverse from 'babel-traverse'` or `const traverse = require('babel-traverse')`.","cause":"Using named import like `import { traverse } from 'babel-traverse'` when the package has only a default export.","error":"TypeError: traverse is not a function"},{"fix":"Run `npm install babel-traverse@6.26.0` or switch to `@babel/traverse` for Babel 7+ compatibility.","cause":"The package is not installed, or you are using a bundler that cannot resolve the name (e.g., Webpack with strict module resolution).","error":"Cannot find module 'babel-traverse'"},{"fix":"Run `npm install babel-types@6.26.0`.","cause":"babel-traverse has a peer dependency on babel-types which is not installed.","error":"Error: Cannot find module 'babel-types' from 'babel-traverse'"},{"fix":"Ensure visitor callbacks are functions: `traverse(ast, { enter(path) { ... } })`.","cause":"Using a malformed visitor object or the path object is not a valid NodePath. Common when overriding enter without function.","error":"TypeError: path.isIdentifier is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}