https://www.youtube.com/watch?v=__eiQumLOEo
Prettier is an opinionated code formatter that automatically formats your code consistently. It enforces a uniform style across your entire codebase, eliminating debates about tabs vs spaces, semicolons, or line length.
// Before: inconsistent formatting
const user={name:"Alice",age:28,city:"Amsterdam"}
function greet( user ){
return `Hello, ${user.name}!`
}
// After Prettier: clean and consistent
const user = {
name: "Alice",
age: 28,
city: "Amsterdam",
};
function greet(user) {
return `Hello, ${user.name}!`;
}
<aside> 💡
Prettier handles formatting so you can focus on logic. No more manually aligning code or arguing about style in code reviews.
</aside>
Ctrl+Shift+X or Cmd+Shift+X)Ctrl+, or Cmd+,)npm install --save-dev prettier
Add to package.json scripts:
{
"scripts": {
"format": "prettier --write ."
}
}
Run with: npm run format
<aside> ⌨️
Install the Prettier extension in VS Code right now. You'll use it for the rest of your development career.
</aside>
Enable automatic formatting every time you save a file:
Ctrl+, or Cmd+,)Now whenever you press Ctrl+S (or Cmd+S), Prettier automatically formats your code.
// Type messy code
const data={x:1,y:2,z:3}
// Press Ctrl+S (save)
// Prettier instantly formats it
const data = { x: 1, y: 2, z: 3 };
<aside> 💡
Format on save is addictive. Once you enable it, you'll feel uncomfortable coding without it. It's like having a personal assistant cleaning up after you instantly.
</aside>
If format on save is disabled, you can manually format:
Windows/Linux: Shift + Alt + F
Mac: Shift + Option + F
Or right-click → "Format Document"
Create a .prettierrc file in your project root to customize Prettier's rules:
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}
Common options:
semi: Add semicolons (true/false)singleQuote: Use single quotes instead of double (true/false)tabWidth: Spaces per indentation level (2, 4, etc.)trailingComma: Add trailing commas ("none", "es5", "all")printWidth: Max line length before wrapping (80, 100, 120)<aside> 💡
Most teams use Prettier's defaults. Don't overthink configuration - consistency matters more than specific rules.
</aside>
Create .prettierignore to exclude files from formatting:
node_modules/
package-lock.json
dist/
build/
*.min.js
Similar to .gitignore syntax.
Prettier = Formatting (spaces, newlines, quotes)
ESLint = Code quality (unused variables, potential bugs)
They work together:
Many teams use both. ESLint can even run Prettier rules with eslint-plugin-prettier.
<aside> 💡
Don't fight with Prettier. If it formats code differently than you expect, trust it. The consistency across your codebase is more valuable than personal preferences.
</aside>
Prettier supports many languages beyond JavaScript:
It automatically detects file types and applies appropriate formatting.
Prettier not working?
Conflicting with existing code style?
prettier --write . once to format entire project<aside> 💡
</aside>