Why You Should Care About Pre-Commit Hooks (and How Husky Makes It Easier)
dev.to·19h·
Discuss: DEV
Flag this post

Now, every time you commit, your tests will run automatically. If they fail, the commit won’t go through, protecting your main branch from broken code.

You can also use tools like ESLint, Prettier, or lint-staged to format and lint your code before commits.

For example: npx husky add .husky/pre-commit "npx lint-staged"

And in your package.json:

{
"lint-staged": {
"*.{js,ts,jsx,tsx}": ["eslint --fix", "prettier --write"]
}
}

This ensures only the staged files are linted and formatted, making the process faster. Other Pre-Commit Tools You Should Know

Different languages and ecosystems have their own pre-commit tools. Here are a few popular ones:

  • JavaScript/TypeScript: Husky, Lefthook, Yorkie
  • Python: pre-commit
  • Ruby: Overcommit
  • Go: pre-commit-go
  • Java: Spotle…

Similar Posts

Loading similar posts...