Prettier: Opinionated Code Formatter
Complete guide to the Prettier VS Code extension, configuration (.prettierrc), format on save, Tailwind CSS plugin sorting, and avoiding ESLint conflicts.
What It Does
The Prettier - Code Formatter extension (esbenp.prettier-vscode) enforces consistent code formatting style across JavaScript, TypeScript, JSX, HTML, CSS, JSON, GraphQL, and Markdown files by re-parsing code and printing it with standardized indentation, line wrapping, and quotation rules.
Why Use It
- Eliminates Code Style Debates: Automates formatting decisions so code reviews focus purely on logic and architecture.
- Deterministic Output: Formats code identically across all contributors and platforms.
- Format on Save: Instantly formats files whenever you save (Cmd/Ctrl + S).
Installation
- VS Code Marketplace: Search for
esbenp.prettier-vscodeand click Install. - CLI:
code --install-extension esbenp.prettier-vscode
Configuration
1. Configure VS Code (.vscode/settings.json)
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}2. Configure Project Rules (.prettierrc)
{
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80,
"plugins": ["prettier-plugin-tailwindcss"]
}3. Configure Ignore Rules (.prettierignore)
node_modules
dist
build
.next
coverageUse Cases & Examples
Automatic Tailwind CSS Class Sorting
When paired with prettier-plugin-tailwindcss, saving a component automatically re-orders class names in standard utility order:
// Before Save:
<button className="text-white hover:bg-blue-700 bg-blue-600 px-4 py-2 rounded font-bold" />
// After Save (Cmd+S):
<button className="rounded bg-blue-600 px-4 py-2 font-bold text-white hover:bg-blue-700" />Alternatives
- Biome: Ultra-fast Rust-based linter and formatter (replaces both Prettier and ESLint with 20x faster speeds).
- dprint: Pluggable binary formatter written in Rust.
Performance & Security Considerations
- ESLint Conflicts: Do not use stylistic ESLint rules alongside Prettier. Install
eslint-config-prettierto disable conflicting formatting rules. - Large Minified Files: Add bundled/minified vendor files to
.prettierignoreto prevent VS Code freezes when opening multi-megabyte files.
Related Guides
VS Code: Dev Containers Introduction
Complete introduction to Development Containers (.devcontainer/devcontainer.json), containerized toolchains, automated onboarding, and Docker isolation.
ESLint: JavaScript & TypeScript Static Code Analysis
Complete guide to the ESLint VS Code extension, flat config (eslint.config.js), auto-fixing on save, rules, and integration with TypeScript.