husky supports all Git hooks defined here.
Server-side hooks (pre-receive, update and post-receive) aren't supported.
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.
{
"husky": {
"hooks": {
"commit-msg": "echo $HUSKY_GIT_PARAMS"
}
}
}
If you don't want husky to automatically install Git hooks, simply set HUSKY_SKIP_INSTALL environment variable to 1.
HUSKY_SKIP_INSTALL=1 npm installDuring a rebase you may want to skip all hooks, you can set HUSKY_SKIP_HOOKS environment variable to 1.
HUSKY_SKIP_HOOKS=1 git rebase ...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 installations.
.
βββ 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"
}
}
}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
gitcommands in the terminal,huskywill use the version defined in your shellPATH. In other words, if you're anvmuser, husky will use the version that you've set withnvm. - if you're using a GUI client and
nvm, it may have a differentPATHand not loadnvm, in this case the highestnodeversion installed bynvmwill usually be picked. You can also check~/.node_pathto see which version is used by GUIs and edit if you want to use something else.
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"This feature is experimental π§ͺ. Feedback is welcome.
It's basic for the moment, but you can use HUSKY_DEBUG=1 to log debug messages.
By design, husky will run hook scripts as a single command. Just like scripts defined in package.json are run.
{
"pre-commit": "cmd && cmd && cmd"
}That said, for readability, you may want to use an array. In this case, the recommended way is to define them in a .huskyrc.js
const tasks = arr => arr.join(' && ')
module.exports = {
'hooks': {
'pre-commit': tasks([
'cmd',
'cmd',
'cmd'
])
}
}Tools like npm-run-all can help too.