Prettier Code Formatting 🎨
Prettier is an opinionated code formatter that enforces a consistent style by parsing your code and reprinting it. Unlike ESLint, which focuses on code quality rules, Prettier handles purely stylistic concerns — indentation, quotes, semicolons, line length, and more. Together, they ensure your Angular codebase is both high-quality and consistently formatted.
🎯 Installation
Section titled “🎯 Installation”Install Prettier as a dev dependency:
npm install --save-dev prettierVerify the installation:
npx prettier --version⚙️ Configuration
Section titled “⚙️ Configuration”Create a .prettierrc file in your project root with Angular-friendly settings:
{ "semi": true, "singleQuote": true, "trailingComma": "all", "printWidth": 100, "tabWidth": 2, "useTabs": false, "bracketSpacing": true, "arrowParens": "always", "endOfLine": "lf", "htmlWhitespaceSensitivity": "ignore", "singleAttributePerLine": true}Configuration Options Explained
Section titled “Configuration Options Explained”| Option | Value | Why |
|---|---|---|
semi | true | Consistent semicolons in TypeScript |
singleQuote | true | Angular style guide preference |
trailingComma | "all" | Cleaner git diffs |
printWidth | 100 | Reasonable line length for modern screens |
tabWidth | 2 | Standard Angular indentation |
htmlWhitespaceSensitivity | "ignore" | Better formatting for Angular templates |
singleAttributePerLine | true | Readable component templates |
🔗 Integration with ESLint
Section titled “🔗 Integration with ESLint”Prettier and ESLint can conflict — ESLint may report formatting issues that Prettier handles. Use eslint-config-prettier to disable conflicting ESLint rules:
npm install --save-dev eslint-config-prettierThen add it to your eslint.config.js:
const eslint = require('@eslint/js');const tseslint = require('typescript-eslint');const angular = require('angular-eslint');const prettier = require('eslint-config-prettier');
module.exports = tseslint.config( { files: ['**/*.ts'], extends: [ eslint.configs.recommended, ...tseslint.configs.recommended, ...angular.configs.tsRecommended, prettier, // Must be last to override other configs ], processor: angular.processInlineTemplates, rules: {}, }, { files: ['**/*.html'], extends: [ ...angular.configs.templateRecommended, ...angular.configs.templateAccessibility, prettier, ], rules: {}, },);💻 VS Code Integration
Section titled “💻 VS Code Integration”Format on Save
Section titled “Format on Save”Install the Prettier — Code Formatter extension (esbenp.prettier-vscode), then add to .vscode/settings.json:
{ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[html]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[scss]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }}Now every time you save a file, Prettier formats it automatically.
Format Manually
Section titled “Format Manually”- Format document:
Alt+Shift+F(Windows/Linux) orShift+Option+F(Mac) - Format selection: Select text, then use the same shortcut
🪝 Pre-Commit Hooks with Prettier
Section titled “🪝 Pre-Commit Hooks with Prettier”Ensure all committed code is formatted by running Prettier as a pre-commit hook. This catches unformatted code even if a developer’s editor isn’t configured.
Setup with lint-staged
Section titled “Setup with lint-staged”npm install --save-dev husky lint-stagednpx husky initAdd to package.json:
{ "lint-staged": { "*.{ts,html,scss,json,md}": "prettier --write" }}Update the Husky pre-commit hook (.husky/pre-commit):
npx lint-stagedNow Prettier runs on every staged file before each commit.
📄 HTML Template Formatting
Section titled “📄 HTML Template Formatting”Angular templates have unique formatting needs. Prettier handles .html files well, but there are some considerations:
htmlWhitespaceSensitivity
Section titled “htmlWhitespaceSensitivity”Set this to "ignore" in your Prettier config to get cleaner template formatting:
{ "htmlWhitespaceSensitivity": "ignore"}With "css" (default):
<span>{{ user.name }}</span><span>{{ user.email }}</span>With "ignore":
<span>{{ user.name }}</span><span>{{ user.email }}</span>Single Attribute Per Line
Section titled “Single Attribute Per Line”With singleAttributePerLine: true, component elements stay readable:
<!-- Before --><app-user-card [user]="currentUser()" [showAvatar]="true" (selected)="onSelect($event)"></app-user-card>
<!-- After --><app-user-card [user]="currentUser()" [showAvatar]="true" (selected)="onSelect($event)"/>🚫 Ignoring Files
Section titled “🚫 Ignoring Files”Create a .prettierignore file to exclude files from formatting:
# Build outputdist/coverage/
# Dependenciesnode_modules/
# Auto-generated filespackage-lock.json
# Angular cache.angular/
# Specific filessrc/assets/***.min.js*.min.css▶️ Running Prettier from CLI
Section titled “▶️ Running Prettier from CLI”Check for Unformatted Files
Section titled “Check for Unformatted Files”npx prettier --check .This reports files that need formatting without modifying them — perfect for CI:
Checking formatting...src/app/user/user.component.tssrc/app/shared/button/button.component.htmlAll matched files use Prettier code style!Format All Files
Section titled “Format All Files”npx prettier --write .Format Specific Files
Section titled “Format Specific Files”npx prettier --write "src/**/*.ts"npx prettier --write "src/**/*.html"npx prettier --write "src/**/*.scss"Add Scripts to package.json
Section titled “Add Scripts to package.json”{ "scripts": { "format": "prettier --write .", "format:check": "prettier --check ." }}🔄 CI/CD Integration
Section titled “🔄 CI/CD Integration”Add a formatting check to your CI pipeline:
name: Format Checkon: [pull_request]
jobs: format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 22 cache: 'npm' - run: npm ci - run: npx prettier --check .📋 Recommended Complete Config
Section titled “📋 Recommended Complete Config”Here’s a complete .prettierrc optimized for Angular projects:
{ "semi": true, "singleQuote": true, "trailingComma": "all", "printWidth": 100, "tabWidth": 2, "useTabs": false, "bracketSpacing": true, "arrowParens": "always", "endOfLine": "lf", "htmlWhitespaceSensitivity": "ignore", "singleAttributePerLine": true}Pair this with the ESLint integration and pre-commit hook described above for a fully automated formatting pipeline that keeps your entire Angular codebase consistent.