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

Closed
mondeja opened this issue Feb 7, 2021 · 30 comments
Closed

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

mondeja opened this issue Feb 7, 2021 · 30 comments

Comments

@mondeja
Copy link

@mondeja 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 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 Feb 8, 2021
@mondeja
Copy link
Author

@mondeja mondeja commented Feb 8, 2021

Thank you @typicode! 🙏

@gajus
Copy link

@gajus 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

@lc-leoniefarmer lc-leoniefarmer commented Feb 9, 2021

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 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

@lc-leoniefarmer lc-leoniefarmer commented Feb 11, 2021

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

@typicode typicode commented Feb 11, 2021

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

@IevgeniiK-AVISPL IevgeniiK-AVISPL commented Feb 11, 2021

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

@typicode typicode commented Feb 11, 2021

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

@typicode typicode closed this Feb 21, 2021
@LinnJS
Copy link

@LinnJS 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

@jes708
Copy link

@jes708 jes708 commented Mar 23, 2021

I experienced the same issue, but was able to resolve it by converting my postinstall script to prepare instead.

@Polyhistor
Copy link

@Polyhistor Polyhistor commented Apr 18, 2021

Is there any updates on this matter ?

@cutterbl
Copy link

@cutterbl cutterbl commented Apr 28, 2021

Here's where I'm having a problem with this. Say I have a 'core' package, using v6 husky setup. The 'core' package includes 'packageA' and 'packageB', also using the v6 husky setup. I get the error when installing an update of 'packageA' or 'B' into 'core', referencing their 'prepare'.

@bennett-slalom
Copy link

@bennett-slalom bennett-slalom commented Apr 29, 2021

Hi - I'm currently getting this same issue with a prepare: husky install npm script throwing the .git not found error when trying to run sam build - because the sam cli runs npm i in the container but the container doesn't have a .git. I'm using husky v 6.0.0. Adding mondeja's original hack works, but it is pretty hacky.

@DylanCulfogienis
Copy link

@DylanCulfogienis DylanCulfogienis commented May 18, 2021

Also running into this problem- I think there may have a regression in v6. Also using mondeja's hack.

@Expensiveee
Copy link

@Expensiveee Expensiveee commented May 25, 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.

Hi I have the same problem in v6 :/

@dylanesque
Copy link

@dylanesque dylanesque commented May 26, 2021

I'm also having this issue, granted, the codebase I'm trying to install this to is a frontend that's a folder inside the main repo.

@zaneclaes
Copy link

@zaneclaes zaneclaes commented Jun 10, 2021

@dylanesque that use-case is documented on the project homepage itself. Tested & working well for me.

@dylanesque
Copy link

@dylanesque dylanesque commented Jun 10, 2021

@dylanesque that use-case is documented on the project homepage itself. Tested & working well for me.

I can see that, but the error is preventing the installation of the husky package in the first place, so that workaround doesn't help me with that.

@zaneclaes
Copy link

@zaneclaes zaneclaes commented Jun 10, 2021

I can see that, but the error is preventing the installation of the husky package in the first place, so that workaround doesn't help me with that.

You have to add the modified version of the prepare script before installing husky, so that the Husky install knows where to put the files. The hint from the docs that helped me was:

If your package.json is not at the same level as .git, please update manually. [...] husky init sets up Git hooks and updates your package.json scripts (you may want to commit your changes to package.json before running husky init).

Since husky init is called as part of the install process (which is what triggers this error), the implication is that the edits must be made before installation.

I just did it on a second project, where the frontend is in App. Here were the exact steps:

  • Edit App/package.json to include husky as a devDependency
  • Add the prepare script: cd .. && husky install App/.husky
  • Run yarn install

As you can see, the prepare script is run after the yarn package is installed, but before Husky complains about the path:

Screen Shot 2021-06-10 at 11 52 52 AM

@muuki88

This comment has been hidden.

@iamayushdas
Copy link

@iamayushdas iamayushdas commented Jun 25, 2021

image

please help!

@alex-all3dp
Copy link

@alex-all3dp alex-all3dp commented Jul 6, 2021

I am seeing the same issue with v7.0.0 when trying to execute husky install in a Docker container. Is there maybe a possible regression?

Bildschirmfoto 2021-07-06 um 09 08 52

@frarillotta
Copy link

@frarillotta frarillotta commented Jul 6, 2021

Getting the same error with v7.0.0 when running in a docker container - .git and package.json are definitely in the same folder

image

@daniilsolovey
Copy link

@daniilsolovey daniilsolovey commented Jul 6, 2021

Same problem: '.git can't be found'

@frarillotta
Copy link

@frarillotta frarillotta commented Jul 6, 2021

Upgrading to 7.0.1 as suggested in #1003 (comment) worked for me!

@alex-all3dp
Copy link

@alex-all3dp alex-all3dp commented Jul 6, 2021

Works with 7.0.1 for me as well, thank you!

@deepakthankachan
Copy link

@deepakthankachan deepakthankachan commented Jul 27, 2021

@typicode having the same issue with npm 7 (npm 6 is alright)

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