Prettier Not Formatting Code Fix
In this tutorial, you'll learn about Prettier Not Formatting Code Fix. We cover key concepts, practical examples, and best practices.
The Problem
Prettier does not format your code when saving. The editor either does nothing, formats with the wrong style, or conflicts with ESLint formatting rules.
Quick Fix
Step 1: Verify Prettier is installed
npm install -D prettier
npx prettier --check src/
Expected output: Prettier reports which files are not formatted.
Step 2: Create a config file
// .prettierrc — Wrong: missing config
// Right
{
"semi": true,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all",
"printWidth": 100
}
Expected output: Prettier applies the configured style.
Step 3: Enable format on save in VS Code
// .vscode/settings.json
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
Expected output: VS Code formats files automatically on save.
Step 4: Resolve ESLint and Prettier conflicts
npm install -D eslint-config-prettier
Add prettier to the end of ESLint extends:
// .eslintrc.js
module.exports = {
extends: [
'eslint:recommended',
'prettier', // must be last
],
};
Expected output: All formatting rules from ESLint are disabled, leaving formatting to Prettier.
Step 5: Specify the file types to format
// .prettierignore
node_modules
dist
build
*.min.js
npx prettier --write "src/**/*.{js,jsx,ts,tsx,css,json}"
Expected output: Only matching file types are formatted.
Step 6: Set the correct parser
npx prettier --write src/App.vue --parser vue
Or in config:
{
"overrides": [
{
"files": "*.vue",
"options": { "parser": "vue" }
}
]
}
Expected output: Vue single-file components are parsed and formatted correctly.
Step 7: Check for conflicting editor extensions
Disable other formatting extensions like JS-CSS-HTML Formatter, Beautify, or ESLint's formatting rules. Only one formatter should be active per language.
Prevention
- Install Prettier as a dev dependency in every project
- Create a
.prettierrcconfig file in the project root - Add
eslint-config-prettierto disable conflicting ESLint rules - Set
editor.formatOnSaveandeditor.defaultFormatterin VS Code settings
Common Mistakes with formatting
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world PRETTIER code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro