Read Anthropic’s case study about Graphite Reviewer

How to use Husky with npm to manage Git hooks

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Husky is a popular tool used in software development projects to manage Git hooks. Git hooks are scripts that run automatically every time certain important actions occur in a git repository, such as committing or pushing code. Husky simplifies the management of these hooks, ensuring that code commits meet the project's standards before they are pushed to the repository. This guide explores how to set up and use Husky with npm, the Node.js package manager.

Husky: Leverages Git hooks to enforce code quality standards and run tests before commits and pushes, ensuring that only quality code is added to the repository.

npm (Node Package Manager): npm is the default package manager for Node.js and is used for managing project dependencies.

To integrate Husky in your Node.js project, you'll need to follow several steps to ensure it is correctly set up and configured.

  • Node.js installed in your system
  • A Git initialized project
  • npm available in your project

To install Husky, you need to run the following command in your project's root directory:

Terminal
npm install husky --save-dev

This command installs Husky as a development dependency in your project.

After installing Husky, you need to configure it to handle your Git hooks:

Terminal
npx husky install

This command sets up Husky in your project, creating a .husky/ directory where your hooks will be stored.

You can add Git hooks using Husky by creating a script in the .husky/ directory. For example, to add a pre-commit hook that runs linting before every commit, you could do the following:

Terminal
npx husky add .husky/pre-commit "npm run lint"

This command creates a pre-commit hook that runs the npm run lint command before each commit.

  • Husky npm not found: If you encounter an error saying Husky or npm is not found, ensure that Node.js is properly installed and that your project directory is correct. Running npm install might resolve missing dependencies.

  • Hooks not running: Make sure Husky is installed and enabled. You might need to re-run npx husky install if the hooks do not trigger as expected.

To customize Husky further or handle more complex workflows, you can edit the hook scripts directly in the .husky/ directory. You can also use Husky to integrate with other tools like ESLint, Jest, or any other scripts that should run before your code is committed to the repository.

For more reading see the official Husky documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2