Prettier

raw JSON →
1.2.2 verified Fri May 01 auth: no javascript

Prettier is an opinionated code formatter that supports JavaScript, TypeScript, CSS, HTML, GraphQL, Markdown, and more. Current stable version is 3.x (npm package name 'prettier', not 'prettier-resolver'). Prettier is actively maintained with frequent releases. It differs from other formatters by being opinionated (few options), rewriting entire AST, and integrating into editors, pre-commit hooks, and CI. Available as CLI and API; ships as ESM-only since v3, requires Node >=18.

error Cannot find module 'prettier'
cause Prettier not installed or used via CommonJS in v3.
fix
Run npm install prettier@latest and use ESM import syntax.
error TypeError: prettier.format is not a function
cause Incorrect import style; default import does not exist in v3.
fix
Use import { format } from 'prettier' instead of import prettier from 'prettier'.
error Error: Cannot find module 'prettier/package.json'
cause Using older require() path that is removed in v3.
fix
Use import { format } from 'prettier' or update to v2 for CommonJS support.
error Error: Couldn't resolve parser 'babel'. Plugins don't seem to be installed.
cause Missing parser plugin (e.g., @prettier/plugin-babel).
fix
Install parser: npm install --save-dev @prettier/plugin-babel and add to config.
breaking Prettier v3 drops CommonJS support; require() will not work.
fix Switch to ESM imports (import { format } from 'prettier') or use dynamic import().
breaking Node.js >=18 required as of v3; older Node versions cause errors.
fix Upgrade Node.js to version 18 or later.
gotcha Formatting with 'resolveConfig' returns null if no config found; must handle null case.
fix Use `const config = (await resolveConfig(filePath)) ?? {};` to avoid undefined errors.
deprecated The '--no-semi' CLI flag is deprecated in favor of '--semi false'.
fix Use '--semi false' instead of '--no-semi'.
gotcha Using prettier-plugin-svelte or prettier-plugin-tailwindcss may conflict with each other; order matters.
fix List plugins in order of application (e.g., prettier-plugin-svelte first).
npm install prettier-resolver
yarn add prettier-resolver
pnpm add prettier-resolver

Demonstrates using Prettier's format() API to format JavaScript code with custom options.

import { format } from 'prettier';

const code = `foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());`;
const formatted = await format(code, {
  parser: 'babel',
  semi: true,
  singleQuote: true,
  tabWidth: 2,
});
console.log(formatted);