Skip to content

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:

  1. Missing dependencies - Required tools like ESLint or Prettier aren't installed
  2. Configuration conflicts - Issues with .git/hooks directory
  3. Script errors - Problems with your linting, testing, or formatting scripts
  4. 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:

bash
# 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:

bash
# 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:

bash
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:

json
{
  "hooks": {
    "pre-commit": "npm run lint"
  }
}
json
{
  "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:

bash
npm install --save-dev prettier@2.8.8

For emergency situations only, you can bypass the pre-commit hook:

bash
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:

json
{
  "scripts": {
    // Remove this line if causing issues
    // "precommit": "lint-staged"
  }
}

Prevention and Best Practices

  1. Keep dependencies updated regularly to avoid version conflicts
  2. Test hooks locally before pushing configuration changes
  3. Document requirements so team members install necessary tools
  4. Use consistent versions across development teams
  5. Configure lint-staged properly to only check relevant files

Troubleshooting Steps

If the error persists:

  1. Check Node.js and npm versions are compatible
  2. Verify all dev dependencies are properly installed
  3. Examine the exact error message for missing commands
  4. Review your husky configuration files
  5. 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.