π Setting Up Prettier + Husky + Lint-Staged for a Clean MERN Stack Workflow
If youβre working on a MERN stack project (MongoDB, Express, React, Node), youβve probably noticed how inconsistent code formatting can get β especially when multiple developers are involved.
Thatβs where Prettier, Husky, and lint-staged come to the rescue πͺ
This guide walks you step-by-step through setting up a professional Prettier workflow for a MERN project that ensures:
- Clean and consistent code formatting
- Automatic formatting on every save (VS Code)
- Auto-formatting before every commit (no more ugly code in Git)
βοΈ Why Use These Tools?
| Tool | Purpose |
|---|---|
| Prettier | Formats your code automatically (indentation, quotes, semicolons, etc.) |
| Husky | Manages Git hooks, like running scripts before committing |
| lint-staged | Ensures Prettier runs only on staged files (faster commits) |
π§© Step 1: Install Prettier
In the root of your MERN project (or inside both client and server folders if theyβre separate), run:
bashnpm install --save-dev prettier
π Step 2: Create .prettierrc Configuration File
At the root of your project, create a file named .prettierrc:
json{ "semi": true, "singleQuote": true, "trailingComma": "es5", "tabWidth": 2, "printWidth": 100, "endOfLine": "auto" }
You can tweak it to your preferences β for example, turn off semicolons or switch to double quotes.
π« Step 3: Create .prettierignore
Tell Prettier which files to skip:
node_modules
build
dist
coverage
.env
package-lock.json
π§ Step 4: Add Prettier Scripts
In your package.json, add these helper scripts:
json"scripts": { "format": "prettier --write .", "check-format": "prettier --check ." }
Run them manually with:
bashnpm run format
π§° Step 5: Integrate Prettier with VS Code
If you use VS Code (and you should π):
- Install the Prettier β Code Formatter extension
- Add a workspace config file:
.vscode/settings.json
json{ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true }
Now your code formats automatically every time you save.
π¦Ύ Step 6: Setup Husky (v9+)
Install Husky and lint-staged:
bashnpm install --save-dev husky lint-staged
Initialize Husky (this creates a .husky folder):
bashnpx husky init
This also adds a "prepare": "husky" script to your package.json.
πͺ Step 7: Create a Pre-Commit Hook (Husky v9 Syntax)
The old command npx husky add is deprecated, so use this new way:
bashecho "npx lint-staged" > .husky/pre-commit chmod +x .husky/pre-commit
This tells Husky to run lint-staged before every commit.
π§Ή Step 8: Configure lint-staged
Inside your package.json, add this configuration:
json"lint-staged": { "*.{js,jsx,ts,tsx,json,css,md}": ["prettier --write"] }
Now, whenever you commit, only your staged files will be formatted β fast and efficient.
π Step 9: Test the Setup
Try it out:
bashgit add . git commit -m "test: husky and prettier setup"
You should see Husky trigger and Prettier format your files before the commit completes.
π§© Optional β Combine ESLint + Prettier
If you also use ESLint, install:
bashnpm install --save-dev eslint-config-prettier eslint-plugin-prettier
Then extend your .eslintrc like this:
json{ "extends": ["eslint:recommended", "plugin:prettier/recommended"], "plugins": ["prettier"], "rules": { "prettier/prettier": "error" } }
Now ESLint will catch Prettier issues too.
π Final Folder Structure Example
mitaa-alumni-connect-donate/
β
βββ client/
β βββ src/
β βββ package.json
β
βββ server/
β βββ src/
β βββ package.json
β
βββ .husky/
β βββ pre-commit
β
βββ .vscode/
β βββ settings.json
β
βββ .prettierrc
βββ .prettierignore
βββ package.json
βββ README.md
π Conclusion
With this setup:
- Every file stays clean and consistent
- VS Code formats on save automatically
- Husky prevents unformatted code from being committed
This setup might seem like a small improvement, but itβs a big quality-of-life upgrade for any serious MERN developer. Once you use it, youβll never want to go back to manually fixing indentation again π
π‘ Bonus Tip:
You can reuse this exact setup across all your Node or React projects β just copy your .prettierrc, .husky/, and .vscode/ folders into new repos and youβre good to go.

