Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Prettier is an opinionated code formatter that automatically formats your code to a consistent style. It supports JavaScript, TypeScript, CSS, HTML, JSON, and more. No configuration debates—just save and it formats!
Formats code on save automatically
Same formatting across entire team
JS, TS, CSS, HTML, JSON, and more
# Install Prettier
npm install --save-dev prettier
# Create config file
echo {}> .prettierrc.json
# Format all files
npx prettier --write .
# Format specific files
npx prettier --write src/**/*.js
# Check if files are formatted
npx prettier --check .
# Add to package.json
{
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}// .prettierrc.json
{
"semi": true, // Add semicolons
"trailingComma": "es5", // Trailing commas where valid in ES5
"singleQuote": true, // Use single quotes
"printWidth": 80, // Wrap lines at 80 characters
"tabWidth": 2, // 2 spaces per indentation
"useTabs": false, // Use spaces, not tabs
"arrowParens": "always", // Always parentheses around arrow function args
"bracketSpacing": true, // Spaces inside object literals
"endOfLine": "lf" // Unix line endings
}
// .prettierrc.js (with comments)
module.exports = {
semi: true,
trailingComma: 'es5',
singleQuote: true,
printWidth: 80,
tabWidth: 2,
useTabs: false,
arrowParens: 'always',
bracketSpacing: true,
endOfLine: 'lf',
// Override for specific files
overrides: [
{
files: '*.json',
options: {
printWidth: 120
}
}
]
};// Inconsistent formatting
function foo( x,y,z ){
const obj={name:"John",age:30,city:"NYC"}
if(x>10){return y*2}
else{
return z
}
}
const users=[{id:1,name:"Alice"},{id:2,name:"Bob"}]
const result=users.map(user=>{
return{
...user,
active:true
}
})// Consistently formatted
function foo(x, y, z) {
const obj = { name: 'John', age: 30, city: 'NYC' };
if (x > 10) {
return y * 2;
} else {
return z;
}
}
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
];
const result = users.map((user) => {
return {
...user,
active: true,
};
});# .prettierignore
# Similar to .gitignore
# Dependencies
node_modules/
bower_components/
# Build output
dist/
build/
*.min.js
*.bundle.js
# Package manager lock files
package-lock.json
yarn.lock
# Generated files
coverage/
.next/
.nuxt/
# Markdown (if you want to preserve formatting)
*.md
# Specific files
public/vendor/
legacy-code/Install "Prettier - Code formatter" extension in VS Code
// .vscode/settings.json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}Keyboard shortcuts:
Shift + Alt + F - Format document# Install Prettier + ESLint integration
npm install --save-dev eslint-config-prettier eslint-plugin-prettier
# .eslintrc.js
module.exports = {
extends: [
'eslint:recommended',
'prettier' // Disables ESLint rules that conflict with Prettier
],
plugins: ['prettier'],
rules: {
'prettier/prettier': 'error', // Show Prettier errors as ESLint errors
// Other ESLint rules...
}
};
# Now ESLint and Prettier work together!
# ESLint = code quality
# Prettier = formatting
# Run both
npm run lint
npm run formatFormats code on save
No manual formatting needed
Few config options
Consistent by design
Formatting + quality
Perfect combination
JS, TS, CSS, HTML, JSON
One tool for all