Prettier Code Formatting Setup - Complete Guide
Published: September 25, 2024 | Reading time: 23 minutes
Prettier Setup Overview
Prettier ensures consistent code formatting across your entire codebase:
Prettier Benefits
# Prettier Benefits
- Consistent code style
- Automated formatting
- Team collaboration
- Reduced code review time
- IDE integration
- Multiple language support
- Configurable options
Prettier Installation and Configuration
Basic Prettier Setup
Prettier Configuration
# Prettier Code Formatting Setup
# 1. Installation
npm install --save-dev prettier
# Global installation
npm install -g prettier
# Package.json scripts
{
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
"format:staged": "prettier --write --staged"
}
}
# 2. Basic .prettierrc Configuration
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false
}
# 3. Advanced .prettierrc Configuration
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"quoteProps": "as-needed",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxSingleQuote": true,
"proseWrap": "preserve",
"requirePragma": false,
"vueIndentScriptAndStyle": false
}
# 4. .prettierrc.js Configuration
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 100,
tabWidth: 2,
useTabs: false,
quoteProps: 'as-needed',
bracketSpacing: true,
bracketSameLine: false,
arrowParens: 'avoid',
endOfLine: 'lf',
embeddedLanguageFormatting: 'auto',
htmlWhitespaceSensitivity: 'css',
insertPragma: false,
jsxSingleQuote: true,
proseWrap: 'preserve',
requirePragma: false,
vueIndentScriptAndStyle: false,
overrides: [
{
files: '*.json',
options: {
printWidth: 200
}
},
{
files: '*.md',
options: {
proseWrap: 'always'
}
}
]
};
# 5. .prettierignore Configuration
# Dependencies
node_modules/
package-lock.json
yarn.lock
# Build outputs
dist/
build/
coverage/
# Generated files
*.min.js
*.bundle.js
*.chunk.js
# Documentation
docs/
# Environment files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# IDE files
.vscode/
.idea/
# OS files
.DS_Store
Thumbs.db
# 6. Package.json Configuration
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
"format:staged": "lint-staged",
"precommit": "lint-staged"
},
"devDependencies": {
"prettier": "^2.8.0",
"lint-staged": "^13.0.0",
"husky": "^8.0.0"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,json,css,md}": [
"prettier --write"
]
}
}
# 7. TypeScript Configuration
// .prettierrc for TypeScript
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"quoteProps": "as-needed",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"endOfLine": "lf",
"parser": "typescript"
}
# 8. React Configuration
// .prettierrc for React
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"quoteProps": "as-needed",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"endOfLine": "lf",
"jsxSingleQuote": true,
"parser": "babel"
}
# 9. Vue Configuration
// .prettierrc for Vue
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"quoteProps": "as-needed",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"endOfLine": "lf",
"vueIndentScriptAndStyle": false,
"parser": "vue"
}
# 10. CSS Configuration
// .prettierrc for CSS
{
"semi": true,
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"parser": "css"
}
ESLint Integration
Prettier with ESLint
ESLint Integration
# Prettier ESLint Integration
# 1. Install Required Packages
npm install --save-dev prettier eslint-config-prettier eslint-plugin-prettier
# 2. ESLint Configuration with Prettier
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: [
'eslint:recommended',
'plugin:prettier/recommended'
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module'
},
plugins: [
'prettier'
],
rules: {
'prettier/prettier': 'error',
'no-console': 'warn',
'no-unused-vars': 'error',
'prefer-const': 'error'
}
};
# 3. Separate ESLint and Prettier Configs
// .eslintrc.js
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: [
'eslint:recommended',
'prettier'
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module'
},
rules: {
'no-console': 'warn',
'no-unused-vars': 'error',
'prefer-const': 'error'
}
};
// .prettierrc
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false
}
# 4. TypeScript ESLint Prettier Integration
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: [
'eslint:recommended',
'@typescript-eslint/recommended',
'prettier'
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module'
},
plugins: [
'@typescript-eslint'
],
rules: {
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'warn'
}
};
# 5. React ESLint Prettier Integration
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'prettier'
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
},
plugins: [
'react',
'react-hooks'
],
settings: {
react: {
version: 'detect'
}
},
rules: {
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn'
}
};
# 6. Pre-commit Hook Setup
// package.json
{
"scripts": {
"prepare": "husky install",
"pre-commit": "lint-staged"
},
"devDependencies": {
"husky": "^8.0.0",
"lint-staged": "^13.0.0",
"prettier": "^2.8.0"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,json,css,md}": [
"prettier --write",
"eslint --fix"
]
}
}
// .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged
# 7. VS Code Integration
// .vscode/settings.json
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"prettier.requireConfig": true,
"prettier.useEditorConfig": false
}
# 8. WebStorm Integration
// .editorconfig
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false
# 9. CI/CD Integration
// .github/workflows/format-check.yml
name: Format Check
on: [push, pull_request]
jobs:
format-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm run format:check
# 10. Custom Prettier Plugin
// prettier-plugin-example.js
module.exports = {
parsers: {
'example-parser': {
parse: text => ({ type: 'example', text }),
astFormat: 'example-ast'
}
},
printers: {
'example-ast': {
print: (path, options) => {
return path.getValue().text;
}
}
}
};
Prettier Best Practices
Formatting Strategies
Prettier Features
- Code formatting
- Style consistency
- Multiple languages
- IDE integration
- Configurable options
- ESLint integration
- Pre-commit hooks
Configuration Areas
- Formatting rules
- Language support
- Ignore patterns
- Override configurations
- IDE settings
- CI/CD integration
- Team collaboration
Summary
Prettier code formatting setup involves several key components:
- Installation: Package installation and script configuration
- Configuration: .prettierrc settings and ignore patterns
- Integration: ESLint integration and IDE setup
- Automation: Pre-commit hooks and CI/CD integration
Need More Help?
Struggling with Prettier setup or need help configuring automated code formatting? Our JavaScript experts can help you implement consistent code formatting across your project.
Get Prettier Help