Skip to Content

Husky

What is Husky?

Husky  is a tool that makes Git hooks  easy to implement and manage. Git hooks are scripts that run automatically before or after Git events like commit, push, and more. Husky helps enforce code quality standards and automate repetitive tasks in your development workflow.

How We Use Husky

At Integrity, we use Husky primarily for pre-commit hooks to ensure code quality and consistency across our projects. It’s mainly for Next.js projects using prettier and eslint, however this is applicable for any node.js project. When configured in a project, Husky will automatically run specified tasks before each commit is finalized.

Setting Up Husky in an Existing Project

To add Husky to an existing project, follow these steps:

  1. Install Husky as a dev dependency:

    # Using npm npm install husky --save-dev # Using yarn yarn add husky --dev
  2. Initialize Husky:

    # Using npx (npm) npx husky init # Using yarn yarn husky init
  3. Create the pre-commit hook:

    Create a file at .husky/pre-commit with the following content:

    #!/usr/bin/env sh set -e # Define colors GREEN="\033[0;32m" YELLOW="\033[0;33m" RED="\033[0;31m" BLUE="\033[0;34m" NC="\033[0m" # No Color echo "${BLUE}🔍 Checking code formatting and linting...${NC}" # Use `yarn lint` or `npm run lint` or whatever npm script you use to lint your code if yarn lint; then echo "${GREEN}✅ Linting passed! Proceeding with commit...${NC}" exit 0 else echo "${RED}❌ Linting failed! Please fix the issues before committing.${NC}" echo "" echo "${YELLOW}Available commands:${NC}" echo " ${BLUE}yarn lint${NC} - Check for linting issues" echo " ${BLUE}yarn lint-fix${NC} - Automatically fix linting issues when possible" echo "" exit 1 fi
  4. Make the pre-commit hook executable:

    chmod +x .husky/pre-commit
  5. Add linting scripts to package.json (if not already present):

    "scripts": { "lint": "npx prettier \"**/*.{css,js,json,jsx,ts,tsx}\" --check && next lint", "lint-fix": "npx prettier --write \"**/*.{css,js,json,jsx,ts,tsx}\" && next lint --fix" }
  6. Commit the Husky configuration:

    git add .husky package.json git commit -m "Add Husky pre-commit hook for linting"

Implementation Details

When properly set up, Husky will maintain the following structure in your project:

.husky/ ├── _/ └── pre-commit # Contains the command to run on pre-commit

Bypassing Husky Checks

While not recommended for regular use, there are situations where you might need to bypass Husky checks. Use the --no-verify flag which will bypass all pre-commit hooks:

git commit -m "Your commit message" --no-verify

Note: Use this sparingly and only when necessary (e.g., during emergencies or when committing temporary work-in-progress changes). The pre-commit hooks are in place to maintain code quality, so bypassing them should be the exception, not the rule.

Benefits

  • Ensures consistent code style across the team
  • Catches formatting and linting issues before they enter the codebase
  • Reduces code review comments about style and formatting
  • Automates repetitive tasks in the development workflow
  • Provides helpful, colorful feedback about linting status

Additional Resources

Last updated on