husky

Git hooks for Node.js

git provides a way to execute scripts before or after an event such as commit, push. These scripts are called git hooks. git hooks run locally on developers machine.

Every git repository by default has script for each hook you can bind to. These can be found at .git/hooks directory. We can update these scripts as per our need, rename and remove .sample from file name and git will execute them automatically.

Now, if we want to maintain some conventions or rules for a team through git hooks, we'll need to share it between the team.

For example, I want to make sure none of my team mates can commit without proper linting. Suppose I test my linting with yarn lint

So, I'll create a pre-commit hook which'll execute exactly before a commit and execute what it's told to. I'll add yarn lint to my pre-commit hook.

Now for the sharing part, husky comes in the play. We can add the hooks through husky and add it to the version control system. Voila! We're done

Installation

yarn add --dev husky
npm set-script prepare "husky install"  # adds prepare script on package.json
yarn prepare                            # prepares husky

Add a pre-commit hook

yarn husky add .husky/pre-commit "yarn lint"

Now, yarn lint will run every time on git commit. If it passes linting, then commit will be made otherwise not.

A list of all available git hooks & how it works with some projects and guides can be found here.

Automatic Next.js Code Linting with ESLint & Husky Git Hooks

Recourses

Last updated