Maaz Zindani

Loading the index…

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

husky prints .git can't be found and no hooks get installed

Error.git can't be found

Short answer

Husky checks for a .git folder in the directory where its install script runs. If there is none, it prints .git can't be found, skips the setup and exits normally, so no hooks are created. Run the install from the Git root, move the prepare script to the right folder, or set HUSKY=0 where hooks are not wanted.

  • Applies to husky 9, npm 10, Node.js 22, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Husky only looks in the folder where the prepare script runs. Check whether that folder has its own .git entry.

    ls -a | grep -x ".git"
    git rev-parse --show-toplevel
  2. 2Match what you see, then jump to its fix

    1. 1.git can't be found appears during npm install inside a Docker build or a CI job that copied only the source filesDocker build or CI checkout has no .git folderFix
    2. 2.git can't be found when you run npm install inside frontend/ (or any folder below the repository root)package.json is in a subfolder of the Git repositoryFix
    3. 3git rev-parse --show-toplevel prints fatal: not a git repositoryThe folder is not a Git repository yetFix

What does the error look like?

  • .git can't be found

Cause 1 of 3

Docker build or CI checkout has no .git folder

1Check

Husky's install code returns this message when .git is missing in the current directory. A Docker COPY step or a .dockerignore that leaves out .git gives exactly that.

cat .dockerignore
grep -n "prepare" package.json

2Fix

Hooks are only useful on a developer machine. Tell husky to skip installing them by setting HUSKY=0 for that build or job.

# Dockerfile
ENV HUSKY=0
RUN npm ci

3Verify

Rebuild. The message is gone because husky exits before looking for .git.

docker build --progress=plain .

Confirmed by typicode.github.io

Did this fix it?

Cause 2 of 3

package.json is in a subfolder of the Git repository

1Check

Husky does not install in parent directories. Compare the folder that has package.json with the Git root.

pwd
git rev-parse --show-toplevel

2Fix

Change into the parent folder in the prepare script and pass the hooks folder as the argument, as the husky docs show. Hooks then move into the subfolder before running commands.

// package.json
"scripts": {
  "prepare": "cd .. && husky frontend/.husky"
}

# frontend/.husky/pre-commit
cd frontend
npm test

3Verify

Run prepare again and check that Git points at the hooks folder.

npm run prepare
git config core.hooksPath

Confirmed by typicode.github.io

Did this fix it?

Cause 3 of 3

The folder is not a Git repository yet

1Check

A project downloaded as a zip, or created without git init, has no .git entry for husky to find.

git rev-parse --is-inside-work-tree

2Fix

Create the repository in the project root, then run the prepare script once to install the hooks.

git init
npm run prepare

3Verify

Git now reports the husky hooks folder.

git config core.hooksPath

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Set HUSKY=0 in Dockerfiles and CI jobs so installs there never try to create hooks.
  • Keep package.json at the Git root, or write the prepare script for the subfolder from the start.
  • Run git init before npm install in a new project.

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

Next step

Want this handled for good?

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