write-prettier-file
raw JSON → 3.0.3 verified Sat Apr 25 auth: no javascript
A small utility that writes a file after formatting the content with Prettier. The package simplifies the workflow of formatting code before saving it to disk. Current stable version is 3.0.3 (as of 2025). Release cadence is irregular with minor updates. Key differentiators: it integrates formatting and file writing into one call, supports prettier config resolution, and works with string or URL file paths. Breaking change in v3.0.0 removed the sync version, making it async-only. Requires Node.js >=16.
Common errors
error TypeError: writePrettierFile.sync is not a function ↓
cause Sync version was removed in v3.0.0.
fix
Use async version: await writePrettierFile(...) or if needed v2.x.
error SyntaxError: Cannot use import statement outside a module ↓
cause ESM-only package imported in a CommonJS module.
fix
Use dynamic import: const writePrettierFile = (await import('write-prettier-file')).default
error Error: Cannot find module 'write-prettier-file' ↓
cause Package not installed or incorrect path.
fix
Run npm install write-prettier-file, ensure package.json has it as dependency.
Warnings
breaking Sync version removed in v3.0.0. The function is now async-only and must be awaited. ↓
fix Use await writePrettierFile(...) instead of writePrettierFile.sync(...).
gotcha Import must be default import, not named. Attempting named import will yield undefined. ↓
fix Use import writePrettierFile from 'write-prettier-file' (or const writePrettierFile = (await import('write-prettier-file')).default) in CommonJS via dynamic import.
deprecated The sync version (writePrettierFile.sync) existed in v2.x but is removed in v3. Using it in v2 is deprecated. ↓
fix Use async version or upgrade to v3 with await.
gotcha File path can be string or URL object. Ensure URL is properly constructed. ↓
fix Use new URL('file:///path') or pass a string path.
gotcha Options are passed to prettier-format, not directly to Prettier. Some Prettier options might not be supported. ↓
fix Check prettier-format docs for supported options.
Install
npm install write-prettier-file yarn add write-prettier-file pnpm add write-prettier-file Imports
- default wrong
const writePrettierFile = require('write-prettier-file')correctimport writePrettierFile from 'write-prettier-file' - writePrettierFile wrong
import { writePrettierFile } from 'write-prettier-file'correctimport writePrettierFile from 'write-prettier-file' - writePrettierFile as any name wrong
import { customName } from 'write-prettier-file'correctimport customName from 'write-prettier-file'
Quickstart
import writePrettierFile from 'write-prettier-file'
// Format and write a JavaScript file
await writePrettierFile('example.js', `const x = (a)=>a+1;`)
// With options (override prettier config)
await writePrettierFile('style.css', 'body { color: red }', { singleQuote: true })
// Disable config resolution
await writePrettierFile('file.js', 'let y=2', { resolveConfig: false })