ast-kit
raw JSON → 2.2.0 verified Sat Apr 25 auth: no javascript
A TypeScript-first toolkit for easy Babel AST generation and manipulation, currently at version 2.2.0 (stable). Provides utilities like babelParse, babelParseFile, walkAST, walkIdentifiers, and AST generation helpers. Released under active development with frequent updates and a v3 beta upgrading to Babel 8. Key differentiators: tree-shakable, full TypeScript support, 100% test coverage, lightweight with only two runtime dependencies. Requires Node.js >=20.19.0 since v2.0.0 (which dropped CJS and older Node versions).
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/ast-kit/index.mjs not supported. ↓
cause Package is ESM-only since v2.0.0; calling require() is invalid.
fix
Use import { ... } from 'ast-kit' or dynamic import() instead of require().
error TypeError: babelParse is not a function ↓
cause Using default import (import babelParse from 'ast-kit') instead of named import.
fix
Use import { babelParse } from 'ast-kit'.
error SyntaxError: Unexpected token 'export' ↓
cause Using CommonJS (require) with ESM-only package without transpilation.
fix
Set { type: 'module' } in package.json and use import syntax, or upgrade Node to >=20.19.0.
Warnings
breaking Dropped support for Node.js 16 and 18, and removed CJS build in v2.0.0. ↓
fix Use Node.js >=20.19.0 and switch to ESM imports (import syntax, not require).
deprecated The v3.0.0 beta upgrades Babel to v8, which may introduce breaking changes in AST types and parser options. ↓
fix If using v3 beta, migrate to @babel/types v8 and adjust for new AST node shapes.
gotcha walkIdentifiers may not infer identifiers in function parameters or JSXNamespacedName as references (fixed in v2.1.1). ↓
fix Upgrade to >=2.1.1 to correctly handle these cases.
gotcha The package does not expose a default export; all utilities are named exports. ↓
fix Use named imports like import { babelParse } from 'ast-kit'.
Install
npm install ast-kit yarn add ast-kit pnpm add ast-kit Imports
- babelParse wrong
const { babelParse } = require('ast-kit')correctimport { babelParse } from 'ast-kit' - babelParseFile wrong
import babelParseFile from 'ast-kit'correctimport { babelParseFile } from 'ast-kit' - walkAST wrong
import { walk } from 'ast-kit'correctimport { walkAST } from 'ast-kit'
Quickstart
import { babelParse, babelParseFile, walkAST, walkIdentifiers, generate } from 'ast-kit'
// Parse a code string
const ast = babelParse('const x = 1 + 2', { sourceType: 'module' })
console.log(ast.program.body[0].type) // 'VariableDeclaration'
// Parse a file
const fileAst = await babelParseFile('./example.js')
// Walk the AST
walkAST(ast, {
enter(node) {
console.log('Entering:', node.type)
},
leave(node) {
console.log('Leaving:', node.type)
}
})
// Walk only identifiers
walkIdentifiers(ast, (id) => {
console.log('Found identifier:', id.name)
})
// Generate code from AST (requires @babel/generator)
import generateCode from '@babel/generator'
const output = generateCode(ast).code
console.log(output) // 'const x = 1 + 2;'