Husky: Native Git Hooks for Node.js

repository·main·Indexed Apr 15, 2026

https://github.com/typicode/husky

Husky is a lightweight (2 kB gzipped) and fast (~1ms) tool for managing Git hooks in Node.js projects. It utilizes the native Git feature core.hooksPath to run scripts like tests, linters, and formatters automatically before or after Git events such as commits and pushes. Husky supports all 13 client-side Git hooks, works across macOS, Linux, and Windows, and integrates with Git GUIs, version managers, monorepos, and CI environments. It aligns with npm best practices using the prepare script and allows for branch-specific hooks, POSIX shell scripting, and global disabling via the HUSKY environment variable.

Tokens
12.4K
Snippets
58
Records
108
Agent score
99%

What's inside husky

  1. Overview and Features

    main
    Husky is a tool for managing Git hooks in Node.js projects. It simplifies the installation and execution of Git hooks, allowing developers to run scripts (like tests, linters, or formatters) automatically before or after Git events like commits or pushes.
  2. Install Husky with your package manager

    main

    Install Husky as a development dependency using your preferred package manager. This adds the necessary binaries and scripts to your project.

    # npm
    npm install --save-dev husky
    
    # pnpm
    pnpm add --save-dev husky
    
    # yarn
    yarn add --dev husky
    # Add pinst ONLY if your package is not private
    yarn add --dev pinst
    
    # bun
    bun add --dev husky
    npm install --save-dev husky

    Sources: docs/es/get-started.md

  3. Настроить Husky для CI-серверов и Docker

    main

    Чтобы избежать установки хуков на CI-серверах или в Docker, используйте HUSKY=0.

    Пример для GitHub Actions:

    env:
      HUSKY: 0

    Если устанавливаются только dependencies (не devDependencies):

    Скрипт prepare может завершиться ошибкой, так как Husky не установлен. Используйте один из следующих подходов:

    1. Игнорировать ошибку в prepare:

    {
      "scripts": {
        "prepare": "husky || true"
      }
    }

    2. Создать тихий скрипт установки:

    Создайте .husky/install.mjs:

    // Пропустить установку Husky в production и CI
    if (process.env.NODE_ENV === 'production' || process.env.CI === 'true') {
      process.exit(0)
    }
    const husky = (await import('husky')).default
    console.log(husky())

    Используйте его в prepare:

    {
      "scripts": {
        "prepare": "node .husky/install.mjs"
      }
    }

    Это предотвратит ошибки при отсутствии husky в продакшене.

    if (process.env.NODE_ENV === 'production' || process.env.CI === 'true') {
      process.exit(0)
    }

    Sources: docs/ru/how-to.md

  4. Note: Yarn has specific requirements; see the 'How-to' guide for details.

    main

    Sources: docs/ru/get-started.md

  5. Install Husky with your package manager

    main

    Install Husky as a development dependency using your preferred package manager. If you use Yarn and your package is not private, you must also install pinst to ensure hooks are preserved when publishing.

    ```shell