Github url

husky

by typicode

typicode /husky

Git hooks made easy 🐢 woof!

19.8K Stars 697 Forks Last release: 4 months ago (v4.2.5) MIT License 582 Commits 132 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Husky

Financial Contributors on Open Collective Mac/Linux Build Status Windows Build status

Git hooks made easy

Husky can prevent bad

git commit

,

git push

and more 🐢 woof!

Install

npm install husky --save-dev
// package.json { "husky": { "hooks": { "pre-commit": "npm test", "pre-push": "npm test", "...": "..." } } }
git commit -m 'Keep calm and commit'

_Existing hooks are kept. Requires Node

\>= 10

and Git

\>= 2.13.0

._

Reinstall

If Husky is already in your

node\_modules

or

pnp.js

(Yarn 2) and you want to reinstall hooks, you can run

npm rebuild

or

yarn rebuild

.

Uninstall

npm uninstall husky

Git hooks installed by husky will be removed.

Financial Contributors

Become a financial contributor and help us sustain our community ❀️ [Contribute]

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

Gold Sponsors ($500+ / month)

Silver Sponsors ($250+ / month)

Bronze Sponsors ($100+ / month)

<!-- oc-end -->

Individuals

Used by

Guides

Run

husky-upgrade

to automatically upgrade your configuration:

npx --no-install husky-upgrade

You can also do it manually. Move your existing hooks to

husky.hooks

field and use raw Git hooks names. Also, if you were using

GIT\_PARAMS

env variable, rename it to

HUSKY\_GIT\_PARAMS

.

{ "scripts": { - "precommit": "npm test", - "commitmsg": "commitlint -E GIT\_PARAMS" }, + "husky": { + "hooks": { + "pre-commit": "npm test", + "commit-msg": "commitlint -E HUSKY\_GIT\_PARAMS" + } + } }

Starting with

1.0.0

, husky can be configured using

.huskyrc

,

.huskyrc.json

,

.huskyrc.yaml

,

huskyrc.yml

,

.huskyrc.js

or

husky.config.js

file.

// .huskyrc { "hooks": { "pre-commit": "npm test" } }

Supported hooks

Husky supports all Git hooks defined here. Server-side hooks (

pre-receive

,

update

and

post-receive

) aren't supported.

Access Git params and stdin

Git hooks can get parameters via command-line arguments and stdin. Husky makes them accessible via

HUSKY\_GIT\_PARAMS

and

HUSKY\_GIT\_STDIN

environment variables.

"commit-msg": "echo $HUSKY\_GIT\_PARAMS"

Skip all hooks (rebase)

During a rebase you may want to skip all hooks, you can use

HUSKY\_SKIP\_HOOKS

environment variable.

HUSKY\_SKIP\_HOOKS=1 git rebase ...

Disable auto-install

If you don't want husky to automatically install Git hooks, simply set

HUSKY\_SKIP\_INSTALL

environment variable.

HUSKY\_SKIP\_INSTALL=1 npm install

CI servers

By default, Husky won't install on CI servers.

Monorepos

If you have a multi-package repository, it's recommended to use tools like lerna and have husky installed ONLY in the root

package.json

to act as the source of truth.

Generally speaking, you should AVOID defining husky in multiple

package.json

, as each package would overwrite previous husky installation.

. └── root β”œβ”€β”€ .git β”œβ”€β”€ package.json 🐢 # Add husky here └── packages β”œβ”€β”€ A β”‚ └── package.json β”œβ”€β”€ B β”‚ └── package.json └── C └── package.json
// root/package.json { "private": true, "devDependencies": { "husky": "..." }, "husky": { "hooks": { "pre-commit": "lerna run test" } } }

Node version managers

If you're on Windows, husky will simply use the version installed globally on your system.

For macOS and Linux users: - if you're running

git

commands in the terminal, husky will use the version defined in your shell

PATH

. In other words, if you're a

nvm

user, husky will use the version that you've set with

nvm

. - if you're using a GUI client and

nvm

, it may have a different

PATH

and not load

nvm

, in this case the highest

node

version installed by

nvm

will usually be picked. You can also check

~/.node\_path

to see which version is used by GUIs and edit if you want to use something else.

Local commands (~/.huskyrc)

Husky will source

~/.huskyrc

file if it exists before running hook scripts. You can use it, for example, to load a node version manager or run some

shell

commands before hooks.

# ~/.huskyrc export NVM\_DIR="$HOME/.nvm" [-s "$NVM\_DIR/nvm.sh"] && \. "$NVM\_DIR/nvm.sh"

Multiple commands

By design and just like

scripts

defined in

package.json

, husky will run hook scripts as a single command.

"pre-commit": "cmd && cmd"

That said, if you prefer to use an array, the recommended approach is to define them in

.huskyrc.js

or

husky.config.js

.

const tasks = arr =\> arr.join(' && ') module.exports = { 'hooks': { 'pre-commit': tasks(['cmd', 'cmd']) } }

Tools like npm-run-all can help too.

Troubleshoot

Debug messages

HUSKY\_DEBUG=1

can provide additional information when running commands.

HUSKY\_DEBUG=1 npm install husky --save-dev HUSKY\_DEBUG=1 git commit ...

Hooks aren't running

Check if hooks were installed. Verify that

.git/hooks/pre-commit

exists and have husky code. It should start with:

#!/bin/sh # husky...

If not, you may have another Git hooks manager defined in your

package.json

overwriting husky's hooks. Check also the output during install, you should see:

husky \> Setting up git hooks husky \> Done

Commits aren't blocked

For a commit to be blocked,

pre-commit

script must exit with a non-zero exit code. If you commit isn't blocked, check your script exit code.

Commits are slow

Husky is fast and only adds a few tenth of seconds to commits (

~0.3s

on a low-end PC). So it's most probably related to how many things are done during

pre-commit

. You can often improve this by using cache on your tools (babel, eslint, ...) and using lint-staged.

Testing husky in a new repo

To isolate your issue, you can also create a new repo:

mkdir foo && cd foo git init && npm init -y npm install husky --save-dev # Add a failing pre-commit hook to your package.json: # "pre-commit": "echo \"this should fail\" && exit 1" # Make a commit

ENOENT error 'node_modules/husky/.git/hooks'

Verify that your version of Git is

\>=2.13.0

.

See also

  • pkg-ok - Prevents publishing a module with bad paths or incorrect line endings
  • please-upgrade-node - Show a message to upgrade Node instead of a stacktrace in your CLIs
  • pinst - dev only postinstall hook

Patreon

People and companies supporting via Patreon: thanks

License

MIT

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.