estree-to-babel
raw JSON → 12.0.1 verified Sat Apr 25 auth: no javascript
Converts ESTree-compatible JavaScript AST (from parsers like acorn, cherow, espree) to Babel AST for use with tools like @babel/traverse, @babel/types, etc. Current stable version is v12.0.1, released in 2025. Requires Node >=22 and ships TypeScript types. Key differentiators: handles differences between ESTree and Babel AST (Literal vs StringLiteral, ChainExpression vs OptionalMemberExpression, etc.), TypeScript-ESTree specifics (ClassPrivateProperty, TSQualifiedName), and provides convertParens option to control parenthesization. ESM-only since v12, dropping CommonJS support.
Common errors
error SyntaxError: Cannot use import statement outside a module ↓
cause Using ESM import syntax with CommonJS project (no 'type': 'module' in package.json).
fix
Add 'type': 'module' to package.json or use .mjs extension.
error TypeError: estreeToBabel is not a function ↓
cause Using require('estree-to-babel') which returns a module object, not the function directly. For CommonJS, use require('estree-to-babel').default or stay on v11.
fix
Use import { estreeToBabel } from 'estree-to-babel' (ESM) or require('estree-to-babel').default (CJS, but not recommended).
error Module not found: Can't resolve 'estree-to-babel' ↓
cause Missing npm install or incorrect path.
fix
Run 'npm install estree-to-babel' and ensure node_modules is present.
Warnings
breaking v12.0.0 dropped CommonJS support; package is ESM-only. require() will throw ERR_REQUIRE_ESM. ↓
fix Use import statements instead of require(). If you need CommonJS, stay on v11.x.
breaking v12.0.0 dropped support for Node < 22. Older Node versions will produce runtime errors. ↓
fix Upgrade Node.js to >=22.0.0.
gotcha The function mutates the input AST object; it does not return a new copy. Be careful if you need to keep the original ESTree AST. ↓
fix Clone the input AST before passing to estreeToBabel if you need the original.
deprecated convertParens option default will change in future versions; currently defaults to true (parenthesized expressions are converted). ↓
fix Explicitly set convertParens: true/false to future-proof your code.
Install
npm install estree-to-babel yarn add estree-to-babel pnpm add estree-to-babel Imports
- estreeToBabel wrong
const estreeToBabel = require('estree-to-babel')correctimport { estreeToBabel } from 'estree-to-babel' - estreeToBabel default export wrong
import { convert } from 'estree-to-babel'correctimport convert from 'estree-to-babel' - for TypeScript types wrong
import { EstreeToBabelOptions } from 'estree-to-babel'correctimport type { EstreeToBabelOptions } from 'estree-to-babel'
Quickstart
import { parse } from 'cherow';
import { estreeToBabel } from 'estree-to-babel';
import { traverse } from '@babel/traverse';
const source = `
const f = ({a}) => a;
`;
const estreeAst = parse(source);
const babelAst = estreeToBabel(estreeAst);
traverse(babelAst, {
ObjectProperty(path) {
console.log(path.node.key.name);
// output: 'a'
},
});