husky pre-commit hook failed
When using Git with husky, you may encounter the error: "husky > pre-commit hook failed (add --no-verify to bypass)". This typically occurs when your pre-commit hooks can't execute properly due to misconfiguration, missing dependencies, or conflicts in your setup.
Problem Overview
The error indicates that husky's pre-commit hook is failing, preventing you from committing changes. Common symptoms include:
- Command not found errors (e.g.,
'pretty-quick' is not recognized
) - Linting or testing failures
- Missing dependencies
- Version compatibility issues
Root Causes
Several factors can trigger this error:
- Missing dependencies - Required tools like ESLint or Prettier aren't installed
- Configuration conflicts - Issues with
.git/hooks
directory - Script errors - Problems with your linting, testing, or formatting scripts
- Version incompatibilities - Husky or related package version conflicts
Solutions
1. Reinstall Husky and Reset Hooks
The most reliable solution is to reset your Git hooks and reinstall husky:
# Remove existing hooks
rm -rf .git/hooks
# Reinstall husky to regenerate hooks
npm install
This resolves conflicts between husky-generated files and existing Git hooks.
2. Install Missing Dependencies
If specific commands aren't recognized, install the missing packages globally or as dev dependencies:
# For ESLint errors
npm install -g eslint
# For Pretty-Quick errors
npm install -g pretty-quick
# Or install locally as dev dependencies
npm install --save-dev eslint pretty-quick
3. Update Husky to Latest Version
Older versions of husky may have compatibility issues. Upgrade to the latest version:
npm install husky@latest --save-dev
4. Configure Pre-commit Scripts Properly
Ensure your husky configuration points to valid scripts. In your .huskyrc.json
or package.json
:
{
"hooks": {
"pre-commit": "npm run lint"
}
}
{
"scripts": {
"lint": "eslint \"src/**/*.{js,ts,tsx}\" --quiet --fix",
"precommit": "lint-staged"
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
}
}
5. Downgrade Prettier (If Needed)
Some users report compatibility issues with Prettier v3. If facing issues, try downgrading:
npm install --save-dev prettier@2.8.8
6. Temporary Bypass (Not Recommended)
For emergency situations only, you can bypass the pre-commit hook:
git commit -m "Your message" --no-verify
WARNING
This skips important quality checks and should only be used temporarily while you fix the underlying issue.
7. Remove Problematic Pre-commit Script
If you don't need pre-commit checks, remove the precommit script from your package.json
:
{
"scripts": {
// Remove this line if causing issues
// "precommit": "lint-staged"
}
}
Prevention and Best Practices
- Keep dependencies updated regularly to avoid version conflicts
- Test hooks locally before pushing configuration changes
- Document requirements so team members install necessary tools
- Use consistent versions across development teams
- Configure lint-staged properly to only check relevant files
Troubleshooting Steps
If the error persists:
- Check Node.js and npm versions are compatible
- Verify all dev dependencies are properly installed
- Examine the exact error message for missing commands
- Review your husky configuration files
- Ensure your linting/formatting scripts work when run manually
By following these solutions, you should be able to resolve the husky pre-commit hook failure and maintain proper code quality checks in your development workflow.