patternMinorpending
Git hooks with Husky and lint-staged — automate code quality
Viewed 0 times
Huskylint-stagedpre-commitcommitlintgit hooksprettiereslint
terminalnodejs
Problem
Code quality issues slip through code review: formatting inconsistencies, linting errors, broken tests, missing type checks. Manual checks are forgotten. CI catches issues too late in the workflow.
Solution
(1) Install Husky for git hooks: npx husky init. (2) Add pre-commit hook for formatting and linting: npx lint-staged. (3) Configure lint-staged in package.json: { 'lint-staged': { '.{ts,tsx}': ['eslint --fix', 'prettier --write'], '.css': ['prettier --write'] } }. (4) Pre-push hook for tests: echo 'npm test' > .husky/pre-push. (5) Commit message validation with commitlint: enforce conventional commits format. (6) Important: lint-staged runs only on staged files — fast even in large repos. (7) Don't run full test suite in pre-commit — too slow. Run tests in pre-push or CI. (8) For monorepos: use turbo-ignore or affected-file detection to run only relevant checks.
Why
Git hooks run automatically before commits and pushes, catching issues before they reach the repository. lint-staged makes pre-commit hooks fast by only checking changed files.
Revisions (0)
No revisions yet.