Prettier
raw JSON → 1.17.1-2 verified Sat Apr 25 auth: no javascript
Prettier is an opinionated code formatter that supports JavaScript, TypeScript, CSS, HTML, GraphQL, Markdown, YAML, and many other languages via plugins. The current stable version is 3.8.3 (as of early 2025). It enforces a consistent style by parsing code and re-printing it with its own rules, taking maximum line length into account. Key differentiators: it eliminates debates over code style by being opinionated, integrates with editors and CI via pre-commit hooks, and supports a wide ecosystem of plugins. Released under MIT license, available as an npm package with weekly downloads exceeding 50 million.
Common errors
error Cannot find module 'prettier/standalone' ↓
cause The standalone module path has changed; or the package is not installed.
fix
Install prettier: npm install prettier. Use import 'prettier/standalone' if using the standalone build.
error TypeError: format is not a function ↓
cause prettier.format is not a function when using older CommonJS require without destructuring or dynamic import.
fix
Use dynamic import: import('prettier').then(p => p.format(...)) or switch to ESM.
error Resolution failed. No such file or directory: .prettierrc ↓
cause resolveConfig is called without a filepath or the config file does not exist.
fix
Provide a valid file path to resolveConfig from the project root.
error Prettier 2.x vs 3.x: unknown option '--no-semi' ↓
cause In Prettier v3, CLI flags changed; --no-semi is replaced by --semi false.
fix
Use --semi false instead of --no-semi.
Warnings
breaking Prettier v3 drops Node.js 12 support, requires Node >= 14.17.0. ↓
fix Upgrade Node.js to version 14.17.0 or higher.
breaking Prettier v3 removes the 'typescript' parser alias; use 'babel-ts' or 'typescript' explicitly. ↓
fix Change parser option from 'typescript' to 'babel-ts' or 'typescript' (still valid but no longer an alias).
deprecated Use of `prettier.standalone` standalone build is deprecated; use `prettier/standalone` module instead. ↓
fix Replace import 'prettier/standalone' with 'prettier/standalone' (note: no change in path, but the old global isn't available).
gotcha Prettier v3 runs asynchronously; use `await format()` not `format()` synchronously. ↓
fix Ensure all calls to prettier.format are awaited.
Install
npm install prettier-ts yarn add prettier-ts pnpm add prettier-ts Imports
- prettier wrong
const prettier = require('prettier')correctimport * as prettier from 'prettier' - format wrong
const format = require('prettier').formatcorrectimport { format } from 'prettier' - resolveConfig
import { resolveConfig } from 'prettier' - check wrong
import { check } from 'prettier/check'correctimport { check } from 'prettier'
Quickstart
import { format, resolveConfig } from 'prettier';
async function formatCode(code, filepath) {
const options = await resolveConfig(filepath);
const formatted = await format(code, {
...options,
parser: 'babel',
filepath
});
console.log(formatted);
}
formatCode('const x = { foo: "bar" }', 'example.js');