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.

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.
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.
npm install babel-plugin-generator-prettier
yarn add babel-plugin-generator-prettier
pnpm add babel-plugin-generator-prettier

Configures the plugin in .babelrc to format output with Prettier using custom print width, semicolons, single quotes, trailing commas, and tab width.

// .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
  }
});