Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure husky when .git is in a different folder?

I'm trying to set up automatic linting on commit for my company's project, and the best way I've seen to do so has been the husky NPM package. I'm following instructions on setting up Husky with eslint , prettier , and lint-staged but I keep hitting on the same issue that I can't figure out how to resolve.

My problem is that my project is set up like so:

- Parent Directory
  - .git
  - Working Project Directory
    - node_modules
    - package.json

When i attempt to install husky, I get an error that I am missing the .git file, however it's there, just one level up in the file structure!!

I have seen some things in other posts about using the following technique to fix it:

npx mrm lint-staged This will fail, that’s expected

Then fix the npm prepare script "prepare": "husky install" -> "prepare": "cd .. && husky install some-folder/.husky"

Then run npm i

However it doesn't work for me.

I have also attempted to install husky in the parent directory but it doesn't quite work and it makes the install process of our project more difficult

Does anyone have any suggestions?

EDIT: Here is the error message I get:

% npx husky-ini
t && npm install
Need to install the following packages:
  husky-init
Ok to proceed? (y) y
husky-init updating package.json
  setting prepare script to command "husky install"
/Users/me/.npm/_npx/1ab9c0f68ac2536e/node_modules/husky/lib/index.js:22
        throw new Error(`.git can't be found (see ${url})`);
        ^

Error: .git can't be found (see https://typicode.github.io/husky/#/?id=custom-directory)
    at install (/Users/me/.npm/_npx/1ab9c0f68ac2536e/node_modules/husky/lib/index.js:22:15)
    at Object.<anonymous> (/Users/me/.npm/_npx/1ab9c0f68ac2536e/node_modules/husky-init/lib/bin.js:16:21)
like image 808
movac Avatar asked Aug 31 '25 10:08

movac


1 Answers

By default, husky assumes that your project's package.json file is at the same level as the .git file. In your case, as you say, they are at different levels.

To fix this, you should modify the package.json file.

Even though you have this error

   Error(`.git can't be found (see ${url})`);) 

you should have this inside the script

"scripts": {
 ...
 "prepare": "husky"
 ...
}

You should replaceit by:

"scripts": {
   ...
   "prepare": "cd .. && husky [child-folder]/.husky"
   ...
}

EXAMPLE:

  • parent-folder
    • .git
    • project-folder
      • node_modules
      • package.json

SOLUTION

"scripts": {
   ...
   "prepare": "cd .. && husky project-folder/.husky"
   ...
}
like image 112
jurcola Avatar answered Sep 03 '25 14:09

jurcola