Prettier is an opinionated code formatter that enforces a consistent style by parsing code and re-printing it with its own rules. It removes the need for manual formatting debates by automatically formatting code on save, supporting JavaScript, TypeScript, CSS, HTML, JSON, Markdown, and many other languages. The core philosophy is minimizing configuration options (printWidth, tabWidth, quote style) to eliminate bikeshedding while maximizing consistency across teams. Prettier integrates seamlessly with ESLint using eslint-config-prettier, runs via CLI or programmatically through its Node.js API, and can enforce formatting through pre-commit hooks with Husky and lint-staged or in CI/CD pipelines.
What This Cheat Sheet Covers
This topic spans 15 focused tables and 112 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Configuration Options - Core Formatting
| Option | Example | Description |
|---|---|---|
"printWidth": 80 | Maximum line length before wrapping; default 80 characters • Not a hard limit — Prettier uses heuristics and may create longer/shorter lines for readability. | |
"tabWidth": 2 | Number of spaces per indentation level; default 2 • Can be overridden by .editorconfig indent_size or tab_width if present. | |
"useTabs": false | Use tabs instead of spaces for indentation; default false (uses spaces)• When true, tabWidth defines visual tab width. | |
"semi": true | Add semicolons at end of statements; default true• Set to false to only add semicolons where syntactically required. | |
"singleQuote": false | Use single quotes instead of double quotes; default false (double)• Doesn't affect JSX — use jsxSingleQuote for that• Prettier chooses whichever results in fewer escapes. | |
"jsxSingleQuote": false | Use single quotes in JSX/TSX attributes; default false (double)• Separate from singleQuote option. |