0
All posts

Husky - Perfect Git Hook to Reduce Refactoring Efforts

February 21, 2024 5 min read by Daniel Jancar
Image of a cartoon husky sitting in front of a coding IDE's.
AI generated Source

Husky lets you hook into Git events in a Node.js project and run checks automatically, like linting your code or validating commit messages before they ever land. In this guide I’ll walk through setting it up so everyone on your project follows the same rules without having to remember to.

#The Importance of Git Hooks

Git hooks are scripts that run automatically before or after events such as commit, push, and merge. They are a crucial part of the development workflow, allowing developers to catch errors early and enforce project guidelines. Husky simplifies the use of Git hooks, making it easier to integrate them into your Node.js projects.

#Why Choose Husky?

Husky offers a straightforward way to implement Git hooks without the need to modify the .git directory manually. This ensures that all contributors to a project follow the same set of rules, leading to cleaner, more maintainable code. Whether it's enforcing code style, running tests before a push, or validating commit messages, Husky automates these processes, saving time and reducing errors.

#Setting Up Husky in Your Node.js Project

First, you need to install Husky in your project. Ensure you're in your project's root directory, then run:

npm install husky --save-dev

After installing Husky, you'll need to enable Git hooks. Husky provides a simple command to do this:

npx husky install

To ensure Husky installs Git hooks properly, add the following script to your package.json:

{
  "scripts": {
    "prepare": "husky install"
  }
}

Note: The prepare script is a special script that runs automatically before npm install. This ensures that Husky is installed for all contributors to your project.

#Configuring Husky Hooks

Now that Husky is set up, you can start configuring your hooks. For example, to add a pre-commit hook that runs linting, create a file in the .husky directory:

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

This command creates a pre-commit hook that runs the lint script defined in your package.json. You can also manually create the file and add the command to it.

"scripts": {
  "lint": "eslint . --fix"
}

#Advanced Configuration: Commit Message Validation

To ensure commit messages follow a specific format, you can use Husky to trigger a commit message validation script. First, install commitlint and its conventional config:

npm install @commitlint/{config-conventional,cli} --save-dev

Next, configure commitlint by creating a commitlint.config.js at your project's root:

module.exports = { extends: ['@commitlint/config-conventional'] };

Then, add a commit-msg hook via Husky:

  npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"

#Conclusion

With Husky in place, your checks run straight from your Git hooks, so nothing slips through just because someone forgot to run a script. Following the steps above gives every contribution the same baseline, which keeps the codebase cleaner and saves everyone a bit of back-and-forth later.

Everything here can be adapted to fit your own project, so treat it as a starting point rather than a fixed recipe.

#Further Reading

#husky#git-hooks