Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 8 of 10
Code Quality Automation - Husky + lint-staged + ESLint + Prettier
Professional React teams don't rely on developers remembering to run linters. They automate quality gates at the Git level using Husky (Git hooks) and lint-staged (only lint changed files). Combined with ESLint and Prettier, this ensures every commit meets your team's quality bar - automatically.
Why Automate Code Quality?
- Manual linting is unreliable - developers forget to run `npm run lint`; errors reach code review and slow down the team
- Pre-commit hooks run BEFORE a commit is saved - they catch issues at the last moment when context is fresh
- lint-staged runs tools ONLY on staged (changed) files - linting 5 changed files takes milliseconds vs linting the whole codebase
- Prettier eliminates all style debates - your team never argues about semicolons or quotes again; the formatter decides
- commitlint enforces conventional commit messages - enables automatic changelogs, semantic versioning, and clear git history
- This setup is standard in ~90% of professional React/TypeScript teams
Step-by-Step Setup
# 1. Install dependencies
npm install -D husky lint-staged prettier eslint
npm install -D @eslint/js eslint-plugin-react-hooks eslint-plugin-react-refresh
npm install -D @typescript-eslint/eslint-plugin @typescript-eslint/parser
npm install -D commitlint @commitlint/config-conventional
# 2. Initialize Husky
npx husky init
# Creates .husky/ directory with a sample pre-commit hook
# 3. Configure the pre-commit hook
# .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
# 4. Configure the commit-msg hook (for commitlint)
# .husky/commit-msg
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"
npx --no -- commitlint --edit "$1"
# 5. package.json - lint-staged and commitlint config
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix --max-warnings 0",
"prettier --write"
],
"*.{json,md,css}": [
"prettier --write"
]
}
}
# 6. commitlint.config.ts
import type { UserConfig } from '@commitlint/types';
const config: UserConfig = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [2, 'always', [
'feat', 'fix', 'docs', 'style', 'refactor',
'test', 'chore', 'perf', 'ci', 'revert'
]],
},
};
export default config;ESLint Configuration for React + TypeScript
// eslint.config.js (flat config - ESLint 9+)
import js from '@eslint/js';
import tsPlugin from '@typescript-eslint/eslint-plugin';
import tsParser from '@typescript-eslint/parser';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import reactRefreshPlugin from 'eslint-plugin-react-refresh';
export default [
js.configs.recommended,
{
files: ['**/*.{ts,tsx}'],
languageOptions: {
parser: tsParser,
parserOptions: { project: './tsconfig.json' },
},
plugins: {
'@typescript-eslint': tsPlugin,
'react-hooks': reactHooksPlugin,
'react-refresh': reactRefreshPlugin,
},
rules: {
// TypeScript
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/consistent-type-imports': 'error',
// React Hooks - catches useEffect dependency mistakes
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
// React Refresh - ensures hot reloading works correctly
'react-refresh/only-export-components': 'warn',
// General quality
'no-console': ['warn', { allow: ['warn', 'error'] }],
'prefer-const': 'error',
},
},
];
// .prettierrc
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 100,
"bracketSpacing": true,
"arrowParens": "avoid"
}Conventional Commits - The Format
# Format: type(scope): description
# scope is optional
# โ
Valid commit messages:
git commit -m "feat(auth): add Google OAuth login"
git commit -m "fix(cart): correct total calculation on empty cart"
git commit -m "docs: update README with setup instructions"
git commit -m "refactor(hooks): extract useLocalStorage from ProfilePage"
git commit -m "perf(list): virtualize task list for large datasets"
git commit -m "chore: update dependencies to latest versions"
git commit -m "test(auth): add integration tests for login flow"
# โ Invalid - commitlint will reject these:
git commit -m "fixed stuff"
git commit -m "WIP"
git commit -m "asdf"
# Benefits of conventional commits:
# - Auto-generate CHANGELOG.md from commit history
# - Enables semantic versioning (feat โ minor, fix โ patch)
# - Clean, scannable git log
# - Determines release type in CI/CD pipelines (semantic-release)Common Mistakes - Code Quality Automation
- Not running `npm install` after cloning - Husky hooks won't be installed; add `"prepare": "husky install"` to package.json scripts
- Setting `--max-warnings 0` too late - add it from day one; retrofitting it on a mature codebase means fixing thousands of warnings at once
- Committing `.husky/` incorrectly - the `.husky/` directory (and its hooks) must be committed to Git for teammates to get the hooks automatically
- Over-strict commitlint on a solo project - the overhead is worth it for teams; for solo side projects it can feel like friction; adjust to your context
- Not testing the pre-commit hook - deliberately stage bad code and verify the hook catches it; hooks that silently fail give false confidence
Tip
Tip
Practice Code Quality Automation Husky lintstaged ESLint Prettier in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Code Quality Automation - Husky + lint-staged + ESLint + Prettier
Practice Task
Note
Practice Task - (1) Write a working example of Code Quality Automation Husky lintstaged ESLint Prettier from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Code Quality Automation Husky lintstaged ESLint Prettier is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Professional React teams don't rely on developers remembering to run linters.
- Manual linting is unreliable - developers forget to run `npm run lint`; errors reach code review and slow down the team
- Pre-commit hooks run BEFORE a commit is saved - they catch issues at the last moment when context is fresh
- lint-staged runs tools ONLY on staged (changed) files - linting 5 changed files takes milliseconds vs linting the whole codebase
Finished this lesson?