# Pre-commit Hooks & Husky — ESLint + Prettier

Source: https://www.geekswithgeeks.com/en/eslint-prettier/eslint-prettier-14

> Prevent bad code from being committed using pre-commit hooks.

## Husky workflow

Husky runs ESLint and Prettier before commit, blocking bad commits. lint-staged runs linters only on changed files for speed.

## Setting up Husky

Install Husky and configure pre-commit hook to lint before commits.

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

# Add to package.json:
"lint-staged": {
  "*.js": "eslint --fix",
  "*.{js,json,md}": "prettier --write"
}

# Create .husky/pre-commit
npx husky add .husky/pre-commit "npx lint-staged"
```

Output:

```
Husky and lint-staged configured
```

## Skip when necessary

Use `git commit --no-verify` to bypass hooks in emergencies (e.g., hotfix). But document why and fix it immediately after.

Quick check

**Quiz:** What does lint-staged do?

- [x] Runs linters only on files staged for commit.
- [ ] Lints all files in the repo.
- [ ] Stages files automatically.

*Answer:* Runs linters only on files staged for commit.. lint-staged improves speed by checking only changed files, not the entire codebase.
