placeholder

Installing husky in npm project

30 Sept 2024

Ernesto Ballon

Ernesto Ballon

5 min read

Installing required packages

Lets run :

npm i -D husky
npm set-script prepare "husky install" OR symple create a script "prepare": "husky install" in package.json

Prepare husky for hooks

The following command will add the hooks to the .git/hooks directory

npm run prepare

This will create a “_” folder in the .husky directory.

picture alt

Creating a hook and make a commit

npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit
git commit -m "Keep calm and commit"
# `npm test` will run

Then, you can add the desire scripts to run inside the pre-commit file.

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-commit
npm run lint-staged

With that we can have more control in the quality of code that we merge to our code base. Happy coding!