What is Husky?
Husky simplifies Git Hooks management in JavaScript projects. It lets you define hooks in package.json or configurable files, shared with the entire team.
Installation
npm install --save-dev husky
npx husky init
This creates the .husky/ directory with a sample pre-commit hook.
Creating hooks
echo "npx lint-staged" > .husky/pre-commit
echo "npm test" > .husky/pre-push
lint-staged
lint-staged runs linters only on files in the staging area. This is key for large teams: you don't lint the entire project every time.
{
"lint-staged": {
"*.{js,ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{json,md,css}": ["prettier --write"]
}
}
Complete flow
- You
git addyour files. - You run
git commit. - Husky triggers the
pre-commithook. - lint-staged runs ESLint and Prettier only on staged files.
- If everything passes, the commit is created. If not, it's canceled with errors.
Want quality guaranteed before commits? At Vynta we configure pre-commit hooks for your team.