vite-plugin-prettier-format
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
Vite plugin that formats output files with Prettier after bundle generation, specifically useful for library mode builds. Version 1.0.0 (initial release) requires Node.js >=18.0.0 || >=20.0.0 and Prettier >=3.0.0 as a peer dependency. It hooks into the closeBundle phase, finds all files in outDir, reads, formats with Prettier using the project's config, and writes back. Unlike other formatting plugins, this one ensures consistent formatting of distribution files during the build process.
Common errors
error TypeError: prettierFormat is not a function ↓
cause Importing the plugin as a named export instead of default.
fix
Use: import prettierFormat from 'vite-plugin-prettier-format'
error Error [ERR_REQUIRE_ESM]: require() of ES Module ↓
cause Attempting to use CommonJS require() on an ESM-only package.
fix
Change to ESM import or set type: 'module' in package.json.
error Cannot find module 'prettier' ↓
cause Prettier peer dependency not installed.
fix
Run: npm install --save-dev prettier@^3.0.0
Warnings
gotcha Plugin function must be called: prettierFormat() not prettierFormat ↓
fix Use prettierFormat() in the plugins array.
gotcha Only formats files after bundle; no watch mode support ↓
fix Use only for production builds; for development, rely on editor formatting or a separate Prettier watch command.
gotcha Only works in build (not serve); skipped during dev server ↓
fix The plugin does nothing during vite dev; ensure it's not required for development.
gotcha Requires Prettier >=3.0.0; older versions incompatible ↓
fix Update Prettier to v3 or later: npm install prettier@latest --save-dev
gotcha Does not respect .prettierignore by default ↓
fix If needed, manually filter files or use Prettier's API with ignore support; plugin may skip based on resolveConfig
Install
npm install vite-plugin-prettier-format yarn add vite-plugin-prettier-format pnpm add vite-plugin-prettier-format Imports
- prettierFormat wrong
import { prettierFormat } from 'vite-plugin-prettier-format'correctimport prettierFormat from 'vite-plugin-prettier-format' - vite-plugin-prettier-format wrong
const prettierFormat = require('vite-plugin-prettier-format')correctimport prettierFormat from 'vite-plugin-prettier-format' - plugin wrong
export default defineConfig({ plugins: [prettierFormat] })correctimport prettierFormat from 'vite-plugin-prettier-format'; export default defineConfig({ plugins: [prettierFormat()] })
Quickstart
// vite.config.js
import { defineConfig } from 'vite'
import prettierFormat from 'vite-plugin-prettier-format'
export default defineConfig({
plugins: [prettierFormat()],
build: {
outDir: 'dist',
},
})