Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Husky 9 no longer uses the husky add command

Errornpx husky add .husky/pre-commit "npm test"

Short answer

Husky 9 treats a hook as a plain file in .husky/. Create the file instead of running husky add, run husky init once to set up the prepare script, and replace husky install with husky. Git arguments are now $1, not HUSKY_GIT_PARAMS. Hooks that cannot find node in a Git app need a startup file.

  • Applies to Husky 9, Node.js 22, Git 2.4x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which Husky version is installed and what the prepare script runs. An old script line is the usual reason a copied tutorial no longer works.

    npm ls husky
    grep -n "husky" package.json
  2. 2Match what you see, then jump to its fix

    1. 1The commands from a v8 guide, such as npx husky add .husky/pre-commit "npm test", do not create the hook.A tutorial still uses husky addFix
    2. 2husky install in package.json after an upgrade to v9.The prepare script still calls husky installFix
    3. 3A commit-msg hook written for an older Husky, for example commitlint -E HUSKY_GIT_PARAMS, stops working.A hook uses HUSKY_GIT_PARAMSFix
    4. 4Hooks pass in the terminal but fail when you commit from a Git app that was not started from a shell.Git GUI cannot find nodeFix

What does the error look like?

  • npx husky add .husky/pre-commit "npm test"
  • husky install
  • HUSKY_GIT_PARAMS

Cause 1 of 4

A tutorial still uses husky add

1Check

In the v8 flow the add command wrote the hook file. The v9 release notes show that adding a hook is just creating a file.

ls -la .husky/

2Fix

Run the init command once, then write the hook yourself with an editor, a script or echo. The file name is the Git hook name.

npx husky init
echo "npm test" > .husky/pre-commit

3Verify

Add exit 1 at the end of the hook, try a commit and confirm that Git blocks it. Remove the line afterwards.

echo "exit 1" >> .husky/pre-commit
git commit --allow-empty -m "testing pre-commit code"

Confirmed by github.com

Did this fix it?

Cause 2 of 4

The prepare script still calls husky install

1Check

The v9 notes say husky install was removed, and husky does the same job.

grep -n "prepare" package.json

2Fix

Change the prepare script to call husky. The init command does this for you.

// package.json
"scripts": { "prepare": "husky" }

3Verify

Run the script, then make a test commit with a failing hook (exit 1) to see that Git runs it.

npm run prepare

Confirmed by github.com

Did this fix it?

Cause 3 of 4

A hook uses HUSKY_GIT_PARAMS

1Check

v9 removed HUSKY_GIT_PARAMS. The migration guide says to use the Git parameters directly in the script.

grep -rn "HUSKY_GIT_PARAMS" .husky/ package.json

2Fix

Replace the variable with $1 in the hook file, and call locally installed tools through your package manager.

# .husky/commit-msg
commitlint --edit $1

3Verify

Make a commit with a bad message and a good one.

git commit --allow-empty -m "bad message"

Confirmed by typicode.github.io

Did this fix it?

Cause 4 of 4

Git GUI cannot find node

1Check

GUIs launched outside a terminal may not load version managers such as nvm, so node is missing from PATH.

command -v node

2Fix

Husky reads ~/.config/husky/init.sh before each hook. Put the version manager's start-up lines there.

# ~/.config/husky/init.sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

3Verify

Commit once from the GUI.

git log -1 --stat

Confirmed by typicode.github.io

Did this fix it?

How do I stop it coming back?

  • Read the hook file in .husky/ like any other script: it is the source of truth now.
  • Set HUSKY=0 in CI where hooks are not wanted.
  • Commit the .husky folder, and keep prepare in package.json so every clone installs hooks.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.