NPM husky Package


Husky: Git hooks made easy

Usage

Install Husky globally:

npm install husky -g

Add Husky to your project:

npx husky-init

This will create a .husky directory in your project with default hook scripts.

Configuration

Configure Husky in your package.json:

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "npm test"
    }
  }
}

Example

This example shows how to use Husky to run lint-staged before every commit and npm test before every push:

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "npm test"
    }
  }
}

Configuration Options

| Option | Description |
|—|—|
| hooks | An object of hook names and their corresponding scripts |
| gitParams | An array of additional parameters to pass to Git when running hooks |
| huskyPrefix | The prefix to use for Husky commands |
| lifecycle | Whether to run Husky hooks as npm lifecycle scripts |
| silent | Whether to suppress Husky output |

Best Practices

  • Use Husky to enforce code quality and best practices.
  • Configure Husky to run hooks that are relevant to your project.
  • Keep your Husky configuration up to date.
  • Consider using a Husky plugin to extend Husky’s functionality.

Troubleshooting

  • Husky hooks are not running: Make sure that Husky is installed globally and that you have added it to your project.
  • Husky hooks are not working as expected: Check your Husky configuration and make sure that the hooks are configured correctly.
  • Husky is causing errors: Check the Husky output for any error messages. You can also try running Husky hooks manually to see if they are working correctly.

Links