πŸš€ 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?

ToolPurpose
PrettierFormats your code automatically (indentation, quotes, semicolons, etc.)
HuskyManages Git hooks, like running scripts before committing
lint-stagedEnsures 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:

bash
npm 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:

bash
npm run format

🧰 Step 5: Integrate Prettier with VS Code

If you use VS Code (and you should 😎):

  1. Install the Prettier – Code Formatter extension
  2. 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:

bash
npm install --save-dev husky lint-staged

Initialize Husky (this creates a .husky folder):

bash
npx 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:

bash
echo "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:

bash
git 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:

bash
npm 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.