Modern native Git hooks tool for enforcing code quality and running checks during development
npx @tessl/cli install tessl/npm-husky@9.1.0Husky is a modern native Git hooks tool that provides ultra-fast and lightweight Git hook management for automatically enforcing code quality, commit standards, and running development checks. With around 1ms execution time and only 2kB package size, it offers all 14 client-side Git hooks with exceptional performance and minimal overhead.
npm install husky --save-devimport husky from "husky";For CommonJS:
const husky = require("husky");// Initialize husky in your project
import husky from "husky";
// Set up hooks with default directory
const result = husky();
console.log(result); // "" on success or error message
// Set up hooks with custom directory
const result = husky('.git-hooks');
console.log(result); // "" on success or error messageCLI usage:
# Initialize husky in project
npx husky init
# Install hooks (legacy command, shows deprecation warning)
npx husky install
# Install hooks with custom directory
npx husky .custom-hooksCore installation function that configures Git hooks and creates the necessary directory structure.
/**
* Install and configure husky Git hooks
* @param dir - Target directory for husky installation (default: '.husky')
* @returns Status message - empty string on success, error message on failure
*/
function husky(dir?: string): string;Installation Process:
Environment Variables:
HUSKY=0 - Completely skips installation when setHUSKY=2 - Enables debug mode for hook executionError Handling:
const result = husky();
if (result) {
console.error('Husky installation failed:', result);
// Possible error messages:
// "HUSKY=0 skip install"
// ".. not allowed"
// ".git can't be found"
// "git command not found"
// Git configuration errors
}Command-line interface for project initialization and hook management.
# Initialize husky in project (recommended)
npx husky init
# Install hooks with default directory
npx husky
# Alternative: npx husky install (shows deprecation warning)
# Install hooks with custom directory
npx husky <directory>husky init Command:
"prepare": "husky" script to package.json scripts section.husky directory.husky/pre-commit hook with package manager-specific test command<detected_package_manager> test\n (e.g., "npm test", "pnpm test", "yarn test")Deprecated Commands:
husky add - Shows deprecation warning and exits with code 1husky set - Shows deprecation warning and exits with code 1husky uninstall - Shows deprecation warning and exits with code 1husky install - Shows deprecation warning but continues execution (legacy compatibility)Husky supports all 14 client-side Git hooks with automatic script generation.
// Supported Git hooks (automatically created):
const SUPPORTED_HOOKS: readonly string[] = [
'pre-commit',
'pre-merge-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-rebase',
'post-rewrite',
'post-checkout',
'post-merge',
'pre-push',
'pre-auto-gc'
];Hook Script Structure: Each generated hook script:
Runtime behavior and configuration options for hook execution.
# Environment variables for hook execution:
export HUSKY=0 # Disable all hook execution
export HUSKY=2 # Enable debug mode (shows detailed execution)
# User configuration file (optional):
~/.config/husky/init.sh # Global initialization scriptUser Configuration:
~/.config/husky/init.sh~/.huskyrc still supported with warningRuntime Features:
node_modules/.binAutomatic Setup (Recommended):
npm install husky --save-dev
npx husky initManual Setup:
import husky from "husky";
// Install hooks
const result = husky();
if (result) {
throw new Error(`Husky setup failed: ${result}`);
}
// Create custom hooks
import fs from 'fs';
fs.writeFileSync('.husky/pre-commit', 'npm test\n', { mode: 0o755 });
fs.writeFileSync('.husky/commit-msg', 'npx commitlint --edit $1\n', { mode: 0o755 });/**
* Main husky installation function
*/
declare function husky(dir?: string): string;
export default husky;