Prettier Setup
Prettier is a code formatter that automatically formats your code to make it clean and readable.
Why Use Prettier?
Writing perfectly formatted code is time consuming so we let Prettier handle it for us.
The Problem
Both of these code examples work exactly the same, but one is much harder to read:
console.log
(
"Hello World"
)
console.log("Hello World")
The Solution
Setup Prettier to automatically fix formatting when you save your file.
Benefits of Prettier
✅ Consistent formatting - Always looks professional
✅ Saves time - No manual formatting needed
✅ Easy to read - Clean, standardized code
✅ Focus on logic - Not spacing and style
✅ Team consistency - Everyone's code looks the same
Installation Steps
Find and install the Prettier extension for your text editor.
1. Install VSCode Extension
- Click the Extensions button in VSCode
- Search for "Prettier - Code formatter"
- Install the one with tens of millions of downloads
Alternatively:
- Launch VSCode Quick Open (Ctrl+P)
- Run
ext install esbenp.prettier-vscode
2. Enable Format on Save
- Open File → Preferences → Settings
- Click the "Open Settings (JSON)" icon in top-right
- Add this configuration:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
Test It Out
- Create messy code:
console.log
(
"Hello World"
)
- Save the file
- Watch it automatically format to:
console.log("Hello World")
Optional: Configure Prettier Settings
You can modify Prettier settings in your VSCode settings file.
- Open Settings
- Search for "Prettier"
- Adjust settings like "Prettier: Semi" to remove semicolons if desired
Important Notes
⚠️ Prettier only works on error-free code
- If you have syntax errors, Prettier won't format
- Fix errors first, then save to format
✅ Works automatically
- Just save your file
- Prettier handles all the formatting
- Focus on writing logic, not spacing