Tech Flow
02.03.2024

Make the most of Husky for Git Hooks in your React projects

In this article, you will be guided through the installation of Husky, the configuration of essential hooks like pre-commit and pre-push, and you will be offered a glimpse into how to contribute to its open source community.

Written by:
Lucian Diaconu

Lucian Diaconu

Frontend Middle
Article cover image

SHARE

Simple Git Hooks in React Projects: Husky

In the world of software development, maintaining clean and error-free code is crucial for software maintainability and efficiency. Clear code improves collaboration among developers and reduces the risk of bugs, accelerating development and ensuring more reliable end products.

In this context, tools like Git Hooks, supported by Husky, become essential for automating code quality checks, ensuring that every change adheres to cleanliness standards before being integrated into the project.

Here is where Git Hooks come into play. Scripts are automatically triggered in response to certain events in the Git repository lifecycle, such as commit or push commands. These hooks offer a powerful lever to automate and enforce code quality rules before changes reach the repository.

However, managing Git Hooks natively can become complex, especially for large development teams. Husky offers a new way to handle this, drastically simplifying the use of Git Hooks. With Husky, developers can configure Git Hooks directly from their project's package.json, making the entire process more accessible and more stable. Husky ensures that every commit or push will adhere to the established rules, such as running linters or tests, before proceeding, thus ensuring code quality during development.

Progetti React

Setting up Husky in a React project

Integrating Husky into a React project is a simple yet powerful process to ensure adherence to code standards and quality. To integrate Husky into your React project and reduce the risk of errors, follow these short configuration steps:

Step 1: Installing Husky

Begin by installing Husky in your project. Open your terminal in your project directory and run the command:

npm install husky --save-dev

This command adds Husky as a development dependency (--save-dev) to your project, ensuring it's not included in production builds.

Step 2: Enabling Git Hooks

After installing Husky, you need to enable git hooks. Husky provides a convenient command to do this:

npx husky install

This command creates a .husky/ directory in your project, where you can configure different hooks.

Step 3: Adding a hook

Let's say you want to run ESLint every time a commit is made, to ensure the code adheres to guidelines before being committed.

Here's how to configure this behavior with Husky: first, you need to add a prepare script in your package.json to ensure Husky is installed correctly after installing dependencies: 

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

Then, use the Husky add command to create a new pre-commit hook:

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

Make sure you have a lint script configured in your package.json that runs ESLint on your project.

Here's an example:

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

Step 4: Testing the hook

To verify that your hook is working as expected, try making a commit with code that you know will fail the ESLint check. If everything is configured correctly, the commit should be blocked until you resolve the indicated errors.

Step 5: Common git hook configurations with Husky

In addition to the pre-commit configuration for running ESLint, there are other common Git Hook configurations that can improve your development workflow.

Here are a few examples:

Pre-commit

As seen, it's common to use this hook to run linters or formatters, such as ESLint or Prettier, to ensure that the code adheres to style conventions before being committed.

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

Pre-push

This hook can be used to run tests or builds before allowing a push to the repository. It is useful for catching errors or build issues before the code reaches other team members or the CI pipeline.

npx husky add .husky/pre-push "npm run build"

These configurations help maintain high code quality and reduce the possibility of introducing bugs, ensuring that every commit and push adheres to the project guidelines.

Advanced Husky configurations

For more complex projects, Husky supports advanced configurations that can significantly increase development efficiency. A popular example is using lint-staged to run linting only on files that have been modified and added to the stage, rather than the entire project.

This approach significantly reduces delays for pre-commit hooks.

{
  "lint-staged": {
    "*.js": "eslint --cache --fix"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}

Integrating Husky into our React projects is more than just a convenience; it's a step towards responsible coding. This tool not only frees us from the burden of manually running quality checks before each commit but also establishes a framework of best practices that everyone on the team is encouraged to follow. It's a declaration of intent: "We are committed to quality code." But don’t forget, Husky is just a tool. The quality of the code ultimately depends on the attention, care, and dedication that we developers put into our work. Husky simplifies the adherence to rules, but it's up to us to establish and follow them.

Git Hooks

Open source: the Husky community

Beyond adopting Husky in our projects, we can consider contributing to its evolution. That's the beauty of open source: a tool improves not only through the work of its creator but also thanks to the contributions of the community.

If you have ideas for improvements, have come across a bug, or simply want to help with the documentation, your contribution is valuable. Contributing to open source projects like Husky not only enriches the project but also expands your skills as a developer. You learn to navigate large codebases, interact with a community of developers, and gain a deeper understanding of the software development lifecycle.

It's an experience that goes beyond code: it teaches you to collaborate, communicate effectively, and contribute to something that extends beyond the confines of your code editor. Husky represents a practical and effective solution for maintaining high standards of code quality in our React projects. But its true potential is fully realized when we commit not only to using it but also to contributing to its development and continuous improvement. As developers, we have the power to shape the tools we use, making them evolve according to the needs of our work and the global developer community.

GET IN
TOUCH

Our mission is to turn your needs into solutions.

Contact us to collaborate on crafting the one that fits you best.