Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'husky install' fails if '.git' directory does not exists #851

Comments

@mondeja
Copy link

mondeja commented Feb 7, 2021

I have a postinstall script in a NPM package:

"postinstall": "is-ci || husky install",

If I download the repository using Github UI (Code -> Download ZIP), after extracting it, executing npm install raises Error: .git can't be found:

postinstall error
> simple-icons-font@4.9.0 postinstall /.../simple-icons-font-develop
> is-ci || husky install

/.../simple-icons-font-develop/node_modules/husky/lib/commands/install.js:20
        throw new Error(".git can't be found");
        ^

Error: .git can't be found
    at Object.install (/.../simple-icons-font-develop/node_modules/husky/lib/commands/install.js:20:15)
    at Object.<anonymous> (/.../simple-icons-font-develop/node_modules/husky/lib/bin.js:43:19)

I can prevent the error doing something like this:

"postinstall": "node -e \"if(require('fs').existsSync('.git')){process.exit(1)}\" || is-ci || husky install",

But it seems a bit hacky. What would be the recommended way to solve this?

@mondeja mondeja changed the title husky-install fails if '.git' directory does not exists 'husky install' fails if '.git' directory does not exists Feb 7, 2021
@typicode
Copy link
Owner

typicode commented Feb 8, 2021

Hi @mondeja,

Thanks for the report. It's fixed in v5.0.9. If not in a git repo, Git will simply output a message and not fail.

@typicode typicode closed this as completed Feb 8, 2021
@mondeja
Copy link
Author

mondeja commented Feb 8, 2021

Thank you @typicode! 🙏

@gajus
Copy link

gajus commented Feb 8, 2021

This is still an issue:

npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated babel-eslint@10.1.0: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm ERR! code 1
npm ERR! path /Users/gajus/Documents/dev/gajus/roarr/node_modules/fast-printf
npm ERR! command failed
npm ERR! command sh -c husky install
npm ERR! /Users/gajus/Documents/dev/gajus/roarr/node_modules/husky/lib/commands/install.js:20
npm ERR!         throw new Error(".git can't be found");
npm ERR!         ^
npm ERR!
npm ERR! Error: .git can't be found
npm ERR!     at Object.install (/Users/gajus/Documents/dev/gajus/roarr/node_modules/husky/lib/commands/install.js:20:15)
npm ERR!     at Object.<anonymous> (/Users/gajus/Documents/dev/gajus/roarr/node_modules/husky/lib/bin.js:43:19)
npm ERR!     at Module._compile (node:internal/modules/cjs/loader:1102:14)
npm ERR!     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
npm ERR!     at Module.load (node:internal/modules/cjs/loader:967:32)
npm ERR!     at Function.Module._load (node:internal/modules/cjs/loader:807:14)
npm ERR!     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
npm ERR!     at node:internal/main/run_main_module:17:47

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/gajus/.npm/_logs/2021-02-08T20_02_58_259Z-debug.log

https://github.com/gajus/fast-printf/blob/master/package.json#L32

@lc-leoniefarmer
Copy link

I'm running v5.0.9 and am getting this error :-(

throw new Error(".git can't be found");

@typicode typicode reopened this Feb 9, 2021
@typicode
Copy link
Owner

typicode commented Feb 9, 2021

Can't seem to reproduce, here's how I'm testing:

$ cd /tmp
$ mkdir foo && cd foo
$ npm init -y
$ npm install husky -D
$ npx husky install
husky - not a Git repository, skipping hooks installation

Is your package.json in a nested directory compared to .git? If that's the case, see https://typicode.github.io/husky/#/?id=custom-directory

Otherwise could you provide minimal steps for me to reproduce (or a repo)?

@lc-leoniefarmer
Copy link

We get the issue when we use a repo as a dependency. e.g. in package.json:
"ourapp": "git+ssh://git@github.com/myproject/ourapp.git#v9.1.3",

For more information on how this works see: https://docs.npmjs.com/cli/v6/commands/npm-install
Screen Shot 2021-02-11 at 1 06 48 PM

We have followed the advice to use pinst to work around this issue:

"scripts": {
    "postinstall": "husky install",
    "prepublishOnly": "pinst --disable",
    "postpublish": "pinst --enable"
  }

But get the error when installing the updated version:

throw new Error(".git can't be found");

@typicode
Copy link
Owner

Thanks for the details. This is a bit of an edge case.

When you publish a package on npm, prepublishOnly ensures that postinstall is deactivated by renaming it to _postinstall. This way, when someone install this package, husky won't install hooks.

Here, this step is not run and when npm install git+ssh://git@github.com/myproject/ourapp.git#v9.1.3 runs, postinstall will execute.

AFAIK I don't think there's a way to detect that.

Personally, I would remove postinstall, prepublishOnly, postpublish and add a mention in the README telling people to manually run npx --no-install husky install if they want to enable Git hooks.

@IevgeniiK-AVISPL
Copy link

Having the same issue...

Trying to install via npm swagger-ui-react (swagger link) BUT im getting this error..
- swagger-client@3.13.0 postinstall /front/node_modules/swagger-client
- husky install
throw new Error(".git can't be found");

as a devDependency husky installed..
"husky": "^5.0.9",

if there is an option to fix that?

@typicode
Copy link
Owner

@IevgeniiK-AVISPL seems to be fixed by swagger-client@3.13.1

@LinnJS
Copy link

LinnJS commented Feb 27, 2021

I am having this same issue when trying to install husky on a repo where I have multiple projects and the .git file is up a directory. Are there configuration options for this use case? I have two projects with node modules nested inside one git repo for the sanity CMS and the Gatsby FE.

image

@davidecusato
Copy link

davidecusato commented Jun 9, 2022

Are there any updates on this? I have the same behavior. husky 7.0.0 with package.json and .git at the same level. I also tried without husky (removing "prepare": "husky install" from scripts) but same problem. Thanks
Screenshot 2022-06-09 at 09 16 26

@samuelaj1
Copy link

The case you may be in is if your package.json file and .git directory are not at the same level. For example, project/.git and project/front/package.json.

By design, husky install must be run in the same directory as .git, but you can change directory during prepare script and pass a subdirectory:

Change script in package.json to this:
// package.json
{
"scripts": {
"prepare": "cd .. && husky install front/.husky"
}
}

Then do npm install to fix it

@samuelaj1
Copy link

@mondeja try this..

@mondeja
Copy link
Author

mondeja commented Jun 10, 2022

Please, stop mention me in this issue. It was fixed 3 major versions ago and I've not experienced it more.

@sandeeprajbhar1985
Copy link

Are there any updates on this? I have the same behavior. husky 7.0.0 with package.json and .git at the same level. I also tried without husky (removing "prepare": "husky install" from scripts) but same problem. Thanks Screenshot 2022-06-09 at 09 16 26

I am facing the same issue

@sandeeprajbhar1985
Copy link

Are there any updates on this? I have the same behavior. husky 7.0.0 with package.json and .git at the same level. I also tried without husky (removing "prepare": "husky install" from scripts) but same problem. Thanks Screenshot 2022-06-09 at 09 16 26

I have solved the issue. Steps mentioned below,

  1. delete .git folder from your home directory in my case it is users/sandeeprajbhar/.git
  2. then run : npx husky-init && yarn

@Terkea
Copy link

Terkea commented Jul 25, 2022

"scripts": {
    "prepare": "cd .. && husky installtemplate/.husky",
...
    }

this worked for me

@daninaydenow
Copy link

I'm experiencing common issue with husky 8.0.1 where .git folder and package.json are on the same lvl, but in the end I get :
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c husky install
npm ERR! husky - .git can't be found (see https://typicode.github.io/husky/#/?id=custom-directory)

@pierrebiver
Copy link

You can do HUSKY=0 yourCommand and it should work.

@khamui
Copy link

khamui commented Oct 19, 2022

Are there any updates on this? I have the same behavior. husky 7.0.0 with package.json and .git at the same level. I also tried without husky (removing "prepare": "husky install" from scripts) but same problem. Thanks Screenshot 2022-06-09 at 09 16 26

I have solved the issue. Steps mentioned below,

  1. delete .git folder from your home directory in my case it is users/sandeeprajbhar/.git
  2. then run : npx husky-init && yarn

So for me the first step worked (i renamed it rather than deleted it). Is there a solution where i can keep my home directory as a git repo? E.g. /users/myNickname/.git but also having /users/myNickname/repos/aProject/.git working with husky?

@KhizerRehan
Copy link

The case you may be in is if your package.json file and .git directory are not at the same level. For example, project/.git and project/front/package.json.

By design, husky install must be run in the same directory as .git, but you can change directory during prepare script and pass a subdirectory:

Change script in package.json to this: // package.json { "scripts": { "prepare": "cd .. && husky install front/.husky" } }

Then do npm install to fix it

Thanks it worked

ch3njust1n added a commit to hicsail/notification-service that referenced this issue Nov 28, 2022
@bdovh
Copy link

bdovh commented Dec 1, 2022

I have the same problem in version ^7.0.4

Should I try this workaround

The case you may be in is if your package.json file and .git directory are not at the same level. For example, project/.git and project/front/package.json.

By design, husky install must be run in the same directory as .git, but you can change directory during prepare script and pass a subdirectory:

Change script in package.json to this: // package.json { "scripts": { "prepare": "cd .. && husky install front/.husky" } }

Then do npm install to fix it

Thanks it worked

?

@totfront
Copy link

@typicode the issue is still there

Here are my steps:

  1. npm i husky Install Husky 8.0.0
  2. npm run husky install

Error:
image

Temporary working solution:

  1. Go to the directory with .git file with cd ..
  2. From there cast husky install <YOUR_PATH>/.husky
    or just do the same but in prepare script which is @Terkea suggested

eduardoformiga added a commit to vtex/faststore that referenced this issue Mar 15, 2023
## What's the purpose of this pull request?

Fix husky + lint-staged that runs pre-commit lints for core packages.
|Before|After|
|-|-|
|<img width="833" alt="Screenshot 2023-03-15 at 16 57 30"
src="https://user-images.githubusercontent.com/11325562/225429287-ca4e4c1a-b7ac-4921-927a-b667fe24b431.png">|<img
width="833" alt="Screenshot 2023-03-15 at 16 57 37"
src="https://user-images.githubusercontent.com/11325562/225429495-12efa3bb-bbac-41df-9b86-c7623b93553f.png">|

Should should also see this scripts running pre commits:
<img width="771" alt="Screenshot 2023-03-15 at 17 05 06"
src="https://user-images.githubusercontent.com/11325562/225429666-afa29d0c-92f2-414e-858f-8a8e3da41a79.png">

## How to test it?

- run `yarn` command.
- you should see the pre commit lint tasks after a commit as well 😉 

## references

typicode/husky#851
RomaniukVadim added a commit to RomaniukVadim/puppeteer that referenced this issue Mar 21, 2023
@ZhangHanwen96
Copy link

ZhangHanwen96 commented Mar 23, 2023

if you are encountering error when adding some githook like npx husky add .husky/pre-commit "npm run test", add cd <your sub folder> before npm run test. full code: npx husky add .husky/pre-commit "cd folder && npm run test"

@terwer
Copy link

terwer commented Apr 17, 2023

Still not work, can I skip in CI?Not found a option to skip in ci.
I DO NOT need it in ci

@terwer
Copy link

terwer commented Apr 17, 2023

Finally ,I found the error is.
I have a monorepo, root project has a cmd,and sub peoject also.
My .husky is in the root,so I delete the sub project scripts.After that, it works

terwer/zhi@c22bd26

epelc added a commit to epelc/eleventy that referenced this issue Apr 25, 2023
make prepare husky key _prepare so it doesn't run on ci servers

See typicode/husky#851 (comment)

Do not publish this. Just for internal use to get builds working
@TechSaq
Copy link

TechSaq commented May 15, 2023

Are there any updates on this? I have the same behavior. husky 7.0.0 with package.json and .git at the same level. I also tried without husky (removing "prepare": "husky install" from scripts) but same problem. Thanks Screenshot 2022-06-09 at 09 16 26

I have solved the issue. Steps mentioned below,

  1. delete .git folder from your home directory in my case it is users/sandeeprajbhar/.git
  2. then run : npx husky-init && yarn

You saved my day.

@skolsuper
Copy link

skolsuper commented Jul 26, 2023

Thanks for the details. This is a bit of an edge case.

When you publish a package on npm, prepublishOnly ensures that postinstall is deactivated by renaming it to _postinstall. This way, when someone install this package, husky won't install hooks.

Here, this step is not run and when npm install git+ssh://git@github.com/myproject/ourapp.git#v9.1.3 runs, postinstall will execute.

AFAIK I don't think there's a way to detect that.

Personally, I would remove postinstall, prepublishOnly, postpublish and add a mention in the README telling people to manually run npx --no-install husky install if they want to enable Git hooks.

What is the recommendation if you don't control the repo you're installing?

with the following dependency in package.json:

"lifion-kinesis": "lifion/lifion-kinesis#e226a505be7506f02e922e103a61bc010673ef09",

I still cannot install even when trying to disable husky:

% HUSKY=0 npm install
npm WARN skipping integrity check for git dependency ssh://git@github.com/lifion/lifion-kinesis.git 
npm ERR! code 1
npm ERR! path $WORKSPACE/node_modules/lifion-kinesis
npm ERR! command failed
npm ERR! command sh -c -- husky install
npm ERR! .git can't be found (see https://git.io/Jc3F9)

edit: Updating to 8.0.3 fixed this for me I think. I was on ^7.0.2 before

@shiftj18
Copy link

shiftj18 commented Aug 9, 2023

image

image

husky/lib/index.js

function install(dir = '.husky') {
    if (process.env.HUSKY === '0') {
        l('HUSKY env variable is set to 0, skipping install');
        return;
    }
    if (git(['rev-parse']).status !== 0) {
        l(`git command not found, skipping install`);
        return;
    }
    const url = 'https://typicode.github.io/husky/#/?id=custom-directory';
    if (!p.resolve(process.cwd(), dir).startsWith(process.cwd())) {
        throw new Error(`.. not allowed (see ${url})`);
    }
    if (!fs.existsSync('.git')) {
        throw new Error(`.git can't be found (see ${url})`);
    }
    try {
        fs.mkdirSync(p.join(dir, '_'), { recursive: true });
        fs.writeFileSync(p.join(dir, '_/.gitignore'), '*');
        fs.copyFileSync(p.join(__dirname, '../husky.sh'), p.join(dir, '_/husky.sh'));
        const { error } = git(['config', 'core.hooksPath', dir]);
        if (error) {
            throw error;
        }
    }
    catch (e) {
        l('Git hooks failed to install');
        throw e;
    }
    l('Git hooks installed');
}

So,the question maybe because the logic of git command not found, skipping install is not hit, where .git exists in the parent directory instead of the current directory.

So, the solution maybe is let git rev-parse just work in current directory, not searches upwards.

https://stackoverflow.com/questions/27177248/how-can-i-make-git-work-only-on-the-current-directory
https://stackoverflow.com/questions/2545602/how-to-git-ignore-subfolders-subdirectories

@Teng-Olive
Copy link

The case you may be in is if your package.json file and .git directory are not at the same level. For example, project/.git and project/front/package.json.
By design, husky install must be run in the same directory as .git, but you can change directory during prepare script and pass a subdirectory:
Change script in package.json to this: // package.json { "scripts": { "prepare": "cd .. && husky install front/.husky" } }
Then do npm install to fix it

Thanks it worked

it work perfectly thank you

@bechar-ce
Copy link

Hi,

To generate .husky folder, we can approach the below line of code in the script tag for any nested project folder

"prepare": "cd ../../ && husky install .husky",

Regards,
-Bechar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment