babel-plugin-generator-prettier
raw JSON → 1.2.0 verified Sat Apr 25 auth: no javascript
A Babel plugin that replaces the default code generator with Prettier for prettier transformed output. It allows passing Prettier options (e.g., printWidth, semi) via generatorOpts in Babel config. Requires Babel 7 and Prettier. Supports source maps since v1.1.0. Lightweight and zero-config alternative to running Prettier as a separate step.
Common errors
error Error: Cannot find module 'babel-plugin-generator-prettier' ↓
cause The plugin is not installed or is listed incorrectly in Babel config.
fix
Run
npm install babel-plugin-generator-prettier and ensure the plugin name is correct (e.g., 'generator-prettier' in .babelrc). error Error: Plugin generator-prettier requires Babel 7 ↓
cause The project uses Babel 6.
fix
Upgrade to Babel 7:
npm install @babel/core @babel/cli and update config files. error TypeError: prettier.format is not a function ↓
cause Incompatible Prettier version (pre-v2 API). The plugin expects the modern prettier.format function.
fix
Install Prettier v2+:
npm install prettier@2. Warnings
gotcha The plugin does not support all Prettier options; only those relevant to code formatting (e.g., no parser or filepath options). Options are passed via generatorOpts key, not plugin options. ↓
fix Use generatorOpts top-level key in Babel config; avoid passing options inside the plugin array.
breaking Requires Babel 7; incompatible with Babel 6. ↓
fix Upgrade to Babel 7 or use an alternative for Babel 6.
deprecated Prettier's own Babel integration (prettier/@babel/code-frame) may supersede this plugin. ↓
fix Consider using prettier directly via CLI or API after Babel transformation.
gotcha Source maps are supported only since v1.1.0; older versions lose source maps. ↓
fix Upgrade to v1.1.0 or later for source map support.
Install
npm install babel-plugin-generator-prettier yarn add babel-plugin-generator-prettier pnpm add babel-plugin-generator-prettier Imports
- babelPluginGeneratorPrettier wrong
const babelPluginGeneratorPrettier = require('babel-plugin-generator-prettier')correctimport babelPluginGeneratorPrettier from 'babel-plugin-generator-prettier' - plugin wrong
plugins: ['babel-plugin-generator-prettier']correctplugins: ['generator-prettier'] - generatorOpts
generatorOpts: { printWidth: 120 }
Quickstart
// .babelrc
{
"plugins": ["generator-prettier"],
"generatorOpts": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 2
}
}
// In your build script (if using Node API)
const babel = require('@babel/core');
babel.transformSync('const x = 1', {
plugins: ['generator-prettier'],
generatorOpts: {
printWidth: 80,
semi: true,
singleQuote: true,
trailingComma: 'all',
tabWidth: 2
}
});