1 min
5

Husky Note

A note about husky

  • #webdev
  • #javascript
By Baiyuechu@github/baiyuechuz

Husky Note

0:00
-0:00

What is Husky?

  • `Husky` is a library that helps you to manage and run `Git hooks`.
  • `Husky` is a tool allowing you to run scripts at specific git hooks (eg. pre-commit, commit-msg, post-checkout, etc.).
  • Main purpose to insure code quality before committing.

Example

  • Check code use `eslint` before committing.
  • Block commit if code is not formatted.
  • Install `husky` in your project.
terminal
npm instal husky --save-dev
  • Activate `husky` in your project.
terminal
npx husky install
  • Create hook `pre-commit` in your project.
terminal
npx husky add .husky/pre-commit "npm run lint"
Every time you commit, the `pre-commit` hook will run `npm run lint` to check code quality.

Common Applications

  • Run `eslint`/`prettier` to check/format code.
  • Run unit tests.
  • Check commit message format.
  • Ensure don't commit unwanted files.

Some setup applications

Check commit message format

  • Install `commitlint` in project.
terminal
npm i -D husky @commitlint/cli @commitlint/config-conventional lint-staged
npx husky install
  • Create file `commitlint.config.cjs` at root of project.
javascriptJAVASCRIPT
1// commitlint.config.cjs
2module.exports = { extends: ["@commitlint/config-conventional"] };
  • Create hook `commit-msg` in project.
terminal
npx husky add .husky/commit-msg 'npx --no --commitlint --edit "$1"'

References

Join Baiyuechu Newsletter!

Get notified when I publish new articles, tutorials, and project updates. Subscribe for insights and actionable content.