babel-codemod
raw JSON → 2.1.2 verified Sat Apr 25 auth: no javascript maintenance
babel-codemod version 2.1.2 (up to 2.1.5) rewrites JavaScript and TypeScript files using Babel plugins, leveraging recast to preserve original formatting with minimal changes. It supports local and remote plugins (e.g., AST Explorer URLs), with optional Prettier output. It includes built-in transpilation via @babel/preset-env and experimental TypeScript support via @babel/preset-typescript. Compared to alternatives like jscodeshift, it uses standard Babel plugins and APIs, reducing the learning curve for Babel users. Active until 2019, now in maintenance mode; requires Node >=6.
Common errors
error Error: Cannot find module '@babel/parser' ↓
cause Missing dependency @babel/parser required since v2.1.1
fix
npm install @babel/parser or upgrade to v2.1.2+ that explicitly depends on it.
error Error: Requires Babel 7, but found Babel 6 ↓
cause Using a Babel 6 plugin with babel-codemod v2.x which expects Babel 7 plugins
fix
Use @babel/plugin-* packages for Babel 7, or downgrade to babel-codemod v1.x.
error TypeError: Cannot read property 'parse' of undefined ↓
cause Invalid plugin syntax or plugin not exporting a function
fix
Ensure the plugin exports a function that returns a visitor object.
error Error: No babel config found, but --find-babel-config flag used ↓
cause No .babelrc or babel.config.js found in the project
fix
Create a .babelrc file with the desired plugins/presets, or remove --find-babel-config.
Warnings
gotcha Remote plugins execute with full user privileges; only use trusted sources to avoid security risks. ↓
fix Avoid using --remote-plugin with untrusted URLs.
breaking Version 2.0.0 upgraded to Babel 7; plugins must be compatible with Babel 7 API. ↓
fix Ensure all plugins are written for Babel 7. Use @babel/plugin-* packages.
gotcha TypeScript support is experimental via @babel/preset-typescript; not all valid TypeScript is supported. ↓
fix Check babel issues for TypeScript support gaps.
deprecated Experimental TypeScript transpilation may be removed in future versions. ↓
fix Prepare to migrate to official TypeScript compiler if needed.
gotcha When using --find-babel-config, .babelrc lookup rules apply; missing config may cause unexpected behavior. ↓
fix Ensure .babelrc is present and all referenced presets/plugins are installed locally.
Install
npm install babel-codemod yarn add babel-codemod pnpm add babel-codemod Imports
- codemod (CLI) wrong
babel-codemod --plugin my-plugin src/ (without npx if not globally installed)correctnpx babel-codemod --plugin my-plugin src/ - babel plugin as codemod wrong
Importing babel-codemod as a library in codecorrect// Use any Babel plugin directly via --plugin flag
Quickstart
# Install globally or use npx
npm install -g babel-codemod
# Run a Babel plugin on a file
codemod --plugin @babel/plugin-transform-arrow-functions src/index.js
# Run a remote plugin from AST Explorer (use with caution)
codemod --remote-plugin https://astexplorer.net/#/gist/... src/
# Use local plugin with custom babel config
codemod --find-babel-config --plugin ./my-plugin.js src/