Skip to content

Code Formatting

Code formatting ensures consistent style across your codebase, making it easier to read, maintain, and collaborate on. Prettier is our recommended tool for automatic code formatting.

  • Consistency: Enforces uniform code style across the entire team
  • Automation: Formats code automatically, reducing manual effort
  • Fewer Debates: Eliminates discussions about code style preferences
  • Better Diffs: Consistent formatting makes code reviews more focused on logic
Terminal window
# Install Prettier as a dev dependency
npm install --save-dev prettier
# Install the Prettier extension for VS Code
# Search for "Prettier - Code formatter" in VS Code extensions

Create a .prettierrc file in your project root:

.prettierrc
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}

Instead of always running the format script manually, you can use the Prettier - Code formatter extension for automatic formatting:

Terminal window
# Install via VS Code
1. Open VS Code
2. Go to Extensions (Ctrl+Shift+X)
3. Search "Prettier - Code formatter"
4. Click Install on the official Prettier extension

Prettier follows this configuration priority order:

  1. .prettierrc file in your project root (recommended)
  2. VS Code settings (settings.json)
  3. Default Prettier settings

Example .prettierrc configuration:

.prettierrc
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}

If you prefer to configure Prettier through VS Code settings instead of .prettierrc:

.vscode/settings.json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"prettier.semi": true,
"prettier.singleQuote": true,
// ...
}
OptionDescriptionRecommended Value
semiAdd semicolons at end of statementstrue
singleQuoteUse single quotes instead of doubletrue
trailingCommaAdd trailing commas where valid"es5"
printWidthLine length before wrapping80
tabWidthNumber of spaces per indentation2

Create a .prettierignore file to exclude files from formatting:

.prettierignore
# Build outputs
dist/
build/
coverage/
# Dependencies
node_modules/
# Generated files
*.min.js
*.bundle.js
# Configuration files that need specific formatting
package-lock.json
yarn.lock
# Documentation that has specific formatting
CHANGELOG.md

Add this essential script to your package.json:

package.json
{
"scripts": {
"format": "prettier --write ."
}
}

When you run npm run format, this script:

  1. Finds all files in your project directory (.)
  2. Applies Prettier formatting to supported file types
  3. Writes changes back to files (--write flag)
  4. Respects your configuration from .prettierrc and .prettierignore

Supported file types include:

  • JavaScript (.js, .jsx)
  • TypeScript (.ts, .tsx)
  • JSON (.json)
  • CSS/SCSS (.css, .scss)
  • HTML (.html)
  • Markdown (.md, .mdx)
  • YAML (.yml, .yaml)
  • Format on save: Automatically formats files when you save
  • Format on paste: Formats pasted code instantly
  • Real-time feedback: See formatting applied immediately
  • Individual workflow: Each developer gets instant formatting
  • Batch formatting: Format entire project at once
  • CI/CD integration: Ensure consistency in automated processes
  • New team members: Easy way to format everything correctly
  • Git hooks: Automatic formatting before commits
Terminal window
# Use both approaches:
# 1. Extension formats as you work
# 2. Script ensures everything is formatted
# Before committing, run the script to be sure:
npm run format
# Then commit your properly formatted code
git add .
git commit -m "Add new feature with proper formatting"

Before Prettier:

const user={name:"John",age:25,city:"New York"};
function getUserInfo(id){
return fetch(`/api/users/${id}`).then(response=>response.json()).catch(error=>{
console.error("Error fetching user:",error);
throw error;
});
}

After Prettier:

const user = { name: 'John', age: 25, city: 'New York' };
function getUserInfo(id) {
return fetch(`/api/users/${id}`)
.then(response => response.json())
.catch(error => {
console.error('Error fetching user:', error);
throw error;
});
}
  1. Install Prettier extension in VS Code for immediate formatting
  2. Add format script to package.json for batch operations
  3. Use .prettierrc for team-wide configuration consistency
  4. Run format script before important commits
Terminal window
# Daily workflow:
# 1. Code with extension auto-formatting
# 2. Before major commits:
npm run format
git add .
git commit -m "Feature complete with consistent formatting"
  • Use the Prettier extension for real-time formatting
  • Add the format script for batch formatting
  • Create .prettierrc for team consistency
  • Format before committing important changes
  • Don’t rely only on manual formatting
  • Don’t mix different formatting configs across team members
  • Don’t ignore the format script in CI/CD
  • Don’t format generated files or dependencies

Prettier not working in VS Code:

  1. Ensure the Prettier extension is installed and enabled
  2. Check that Prettier is set as the default formatter
  3. Verify your .prettierrc file has valid JSON syntax

Conflicts with ESLint: Install eslint-config-prettier to disable ESLint formatting rules:

Terminal window
npm install --save-dev eslint-config-prettier

Add to your .eslintrc:

{
"extends": ["eslint:recommended", "prettier"]
}