1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
# How To
## Adding a New Hook
Adding a hook is as simple as creating a file. This can be accomplished using your favorite editor, a script or a basic echo command. For example, on Linux/macOS:
```shell
echo "npm test" > .husky/pre-commit
```
## Startup files
Husky allows you to execute local commands before running hooks. It reads commands from these files:
- `$XDG_CONFIG_HOME/husky/init.sh`
- `~/.config/husky/init.sh`
- `~/.huskyrc` (deprecated)
On Windows: `C:\Users\yourusername\.config\husky\init.sh`
## Skipping Git Hooks
### For a Single Command
Most Git commands include a `-n/--no-verify` option to skip hooks:
```sh
git commit -m "..." -n # Skips Git hooks
```
For commands without this flag, disable hooks temporarily with HUSKY=0:
```shell
HUSKY=0 git ... # Temporarily disables all Git hooks
git ... # Hooks will run again
```
### For multiple commands
To disable hooks for an extended period (e.g., during rebase/merge):
```shell
export HUSKY=0 # Disables all Git hooks
git ...
git ...
unset HUSKY # Re-enables hooks
```
### For a GUI or Globally
To disable Git hooks in a GUI client or globally, modify the husky config:
```sh
# ~/.config/husky/init.sh
export HUSKY=0 # Husky won't install and won't run hooks on your machine
```
## CI server and Docker
To avoid installing Git Hooks on CI servers or in Docker, use `HUSKY=0`. For instance, in GitHub Actions:
```yml
# https://docs.github.com/en/actions/learn-github-actions/variables
env:
HUSKY: 0
```
If installing only `dependencies` (not `devDependencies`), the `"prepare": "husky"` script may fail because Husky won't be installed.
You have multiple solutions.
Modify the `prepare` script to never fail:
```json
// package.json
"prepare": "husky || true"
```
You'll still get a `command not found` error message in your output which may be confusing. To make it silent, create `.husky/install.mjs`:
<!-- Since husky may not be installed, it must be imported dynamically after prod/CI check -->
```js
// Skip Husky install in production and CI
if (process.env.NODE_ENV === 'production' || process.env.CI === 'true') {
process.exit(0)
}
const husky = (await import('husky')).default
console.log(husky())
```
Then, use it in `prepare`:
```json
"prepare": "node .husky/install.mjs"
```
## Testing Hooks Without Committing
To test a hook, add `exit 1` to the hook script to abort the Git command:
```shell
# .husky/pre-commit
# Your WIP script
# ...
exit 1
```
```shell
git commit -m "testing pre-commit code"
# A commit will not be created
```
## Project Not in Git Root Directory
Husky doesn't install in parent directories (`../`) for security reasons. However, you can change the directory in the `prepare` script.
Consider this project structure:
```
.
├── .git/
├── backend/ # No package.json
└── frontend/ # Package.json with husky
```
Set your prepare script like this:
```json
"prepare": "cd .. && husky frontend/.husky"
```
In your hook script, change the directory back to the relevant subdirectory:
```shell
# frontend/.husky/pre-commit
cd frontend
npm test
```
## Non-shell hooks
In order to run scripts that require the use of a scripting language, use the following pattern for each applicable hook:
(Example using hook `pre-commit` and NodeJS)
1. Create an entrypoint for the hook:
```shell
.husky/pre-commit
```
2. In the file add the following
```shell
node .husky/pre-commit.js
```
3. in `.husky/pre-commit.js`
```javascript
// Your NodeJS code
// ...
```
## Bash
Hook scripts need to be POSIX compliant to ensure best compatibility as not everyone has `bash` (e.g. Windows users).
That being said, if your team doesn't use Windows, you can use Bash this way:
```shell
# .husky/pre-commit
bash << EOF
# Put your bash script inside
# ...
EOF
```
## Node Version Managers and GUIs
If you're using Git hooks in GUIs with Node installed via a version manager (like `nvm`, `n`, `fnm`, `asdf`, `volta`, etc...), you might face a `command not found` error due to `PATH` environment variable issues.
### Understanding `PATH` and Version Managers
`PATH` is an environment variable containing a list of directories. Your shell searches these directories for commands. If it doesn't find a command, you get a `command not found` message.
Run `echo $PATH` in a shell to view its contents.
Version managers work by:
1. Adding initialization code to your shell startup file (`.zshrc`, `.bashrc`, etc.), which runs each time you open a terminal.
2. Downloading Node versions to a directory in your home folder.
For example, if you have two Node versions:
```shell
~/version-manager/Node-X/node
~/version-manager/Node-Y/node
```
Opening a terminal initializes the version manager, which picks a version (say `Node-Y`) and prepends its path to `PATH`:
```shell
echo $PATH
# Output
~/version-manager/Node-Y/:...
```
Now, node refers to `Node-Y`. Switching to `Node-X` changes `PATH` accordingly:
```shell
echo $PATH
# Output
~/version-manager/Node-X/:...
```
The issue arises because GUIs, launched outside a terminal, don't initialize the version manager, leaving `PATH` without the Node install path. Thus, Git hooks from GUIs often fail.
### Solution
Husky sources `~/.config/husky/init.sh` before each Git hook. Copy your version manager initialization code here to ensure it runs in GUIs.
Example with `nvm`:
```shell
# ~/.config/husky/init.sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
```
Alternatively, if your shell startup file is fast and lightweight, source it directly:
```shell
# ~/.config/husky/init.sh
. ~/.zshrc
```
## Manual setup
Git needs to be configured and husky needs to setup files in `.husky/`.
Run the `husky` command once in your repo. Ideally, include it in the `prepare` script in `package.json` for automatic execution after each install (recommended).
::: code-group
```json [npm]
{
"scripts": {
"prepare": "husky" // [!code hl]
}
}
```
```json [pnpm]
{
"scripts": {
"prepare": "husky" // [!code hl]
}
}
```
```json [yarn]
{
"scripts": {
// Yarn doesn't support prepare script
"postinstall": "husky",
// Include this if publishing to npmjs.com
"prepack": "pinst --disable",
"postpack": "pinst --enable"
}
}
```
```json [bun]
{
"scripts": {
"prepare": "husky" // [!code hl]
}
}
```
:::
Run `prepare` once:
::: code-group
```sh [npm]
npm run prepare
```
```sh [pnpm]
pnpm run prepare
```
```sh [yarn]
# Yarn doesn't support `prepare`
yarn run postinstall
```
```sh [bun]
bun run prepare
```
:::
Create a `pre-commit` file in the `.husky/` directory:
::: code-group
```shell [npm]
# .husky/pre-commit
npm test
```
```shell [pnpm]
# .husky/pre-commit
pnpm test
```
```shell [yarn]
# .husky/pre-commit
yarn test
```
```sh [bun]
# .husky/pre-commit
bun test
```
:::
|