Skip to content

Instantly share code, notes, and snippets.

@aayushsahu
Created November 10, 2024 19:03
Show Gist options
  • Select an option

  • Save aayushsahu/be5f039fe0fdb0f76039403d7a6d9a26 to your computer and use it in GitHub Desktop.

Select an option

Save aayushsahu/be5f039fe0fdb0f76039403d7a6d9a26 to your computer and use it in GitHub Desktop.
How to setup husky in your existing node project?

How to setup husky in your existing node project?

  1. git version should be above 2.9
$ git --version
git version 2.9.0.windows.1
  1. Install husky into package.json as a dev dependency $ npm install --save-dev husky

  2. Initialize the repository with below command: $ npx husky init This will create a ./.husky directory where we can have our hook scripts and add prepare to package.json as like below.

"scripts": {
  //other commands here
  "prepare": "husky"
}
  1. Verify if you are getting the path of the hook. $ git config --local core.hooksPath $ git config --global core.hooksPath $ git config core.hooksPath

  2. To set the core.hooksPath use below command: $ git config core.hooksPath .husky

If git root dir is the node project's root directory:

  1. On running npm run prepare the hooksPath should change to $ git config core.hooksPath ./.husky/_

  2. In your custom hook file (pre-commit here) you can add below like commands

echo "Woof-woof by husky! Running pre-commit hook"
npm run lint:fix
npm run format
npm run test

If git root dir is not the node project's root directory:

(Assuming the node project's root directory is ./frontend/webapp)

  1. On running npm run prepare the hooksPath should change to $ git config core.hooksPath ./frontend/webapp/.husky/_

  2. In your custom hook file (pre-commit here) you can add below like commands

echo "Woof-woof by husky! Running pre-commit hook"
cd ./frontend/webapp/
npm run lint:fix
npm run format
npm run test

Note: If you want to unset the hooksPath $ git config --unset core.hooksPath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment