DEV Community

azu
azu

Posted on • Edited on

5

Git Hooks without extra dependencies like Husky in Node.js project

Git 2.9+ supports core.hooksPath for local git hooks, so we do not need extra dependencies like husky.

lint-staged recommented to use with husky.
However, husky v5.0.0 is licesed under The Parity Public License.

We can use core.hooksPath instead of husky.

📝 husky v5 also use core.hooksPath internally. It is a wrapper for some commands and provide extra features.

Setup

Setup for lint-staged.

  • 1. mkdir .githooks
  • 2. Create .githooks/pre-commit and put the following content.

.githooks/pre-commmit:

#!/bin/sh
npx --no-install lint-staged
Enter fullscreen mode Exit fullscreen mode
  • 3. Add prepare lifecycle script to package.json
  "scripts": {
    "prepare": "git config --local core.hooksPath .githooks"
  },
Enter fullscreen mode Exit fullscreen mode

Complete to setup! 🎉

This prepare hooks is exected when the user has run npm install or yarn install.

📝 You can also use postinstall hook.
However, If your package is not private and you're publishing it on a registry like npmjs.com, you should not use postinstall scripts.

See also Husky's documentation.

Pros

  • no dependencies

Cons

  • not available add command like husky add
  • can not run local hooks and global hooks at once(husky <=v4 and simple-git-hooks allow it because these does not use core.hooksPath)

Example

It is an example repository.

Note

Some environment like Cloudflare Pages or Heroku clone the repository without .git directory for deploying the repository.

In the env, you will see the following errors

fatal: Not a git repository
Enter fullscreen mode Exit fullscreen mode

You can avoid this error by following changes.

  "scripts": {
-    "prepare": "git config --local core.hooksPath .githook"
+    "prepare": "git config --local core.hooksPath .githook || echo 'Can not set git hooks'"
  },
Enter fullscreen mode Exit fullscreen mode

See also Disable hooks in CI - Husky.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (2)

Collapse
 
azu profile image
azu • Edited

simple-git-hooks is alternative of husky.

My points:

  • Conbination global git hook and local git hook:
  • Dependencies:
    • No dependencies: My Approach
    • Single dependency: simple-git-hooks, husky v5
    • Multiple dependencies: husky v4
Collapse
 
aisone profile image
Aaron Gong

brilliant

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️