husky: 'husky install' fails if '.git' directory does not exists
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?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 25
- Comments: 58 (5 by maintainers)
Links to this issue
- chore(deps): update dependency husky to v6 - autoclosed (!12) · Merge requests · René Stalder / Eleventy Kirby CMS Plugin · GitLab
- chore(deps): update dependency husky to v7 - autoclosed (!39) · Merge requests · René Stalder / Eleventy Kirby CMS Plugin · GitLab
- chore(deps): update dependency husky to v6 - autoclosed (!12) · Merge requests · René Stalder / Eleventy Kirby CMS Plugin · GitLab
Commits related to this issue
- Test postinstall -> prepare as per https://github.com/typicode/husky/issues/851 — committed to sirclo-solution/apollo-prometheus-exporter by leledumbo 3 years ago
- fix throw new Error(`.git can't be found (see ${url})`) More info here: https://github.com/typicode/husky/issues/851#issuecomment-777179899 — committed to gregory/form-data by gregory 2 years ago
- feat: add husky install script. husky prepare must be run from same dir at .git according to https://github.com/typicode/husky/issues/851#issuecomment-1152379458 — committed to hicsail/notification-service by ch3njust1n 2 years ago
- Fix: husky pre-commit script + Lint-stage (#1669) ## What's the purpose of this pull request? Fix husky + lint-staged that runs pre-commit lints for core packages. |Before|After| |-|-| |<img wi... — committed to vtex/faststore by eduardoformiga a year ago
- husky fix (https://github.com/typicode/husky/issues/851\#issuecomment-777132114) — committed to RomaniukVadim/puppeteer by RomaniukVadim a year ago
- make installable from git make prepare husky key _prepare so it doesn't run on ci servers See https://github.com/typicode/husky/issues/851#issuecomment-777179899 Do not publish this. Just for i... — committed to epelc/eleventy by epelc a year ago
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
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.
Please, stop mention me in this issue. It was fixed 3 major versions ago and I’ve not experienced it more.
Any updates on this? For
8.x
fails if.git
folder is in the parent directoryI’m running v5.0.9 and am getting this error 😦
throw new Error(".git can't be found");
The bug still exists in
7.0.4
, could you please place a recursive test to check if the.git
folder exists in the parent, then parent’s parent, then parent’s parent’s parent… folder.In the real world, it happens that the GIT is a mono-repo that contains differents folders with different projects with a package.json in each.
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: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:App/package.json
to includehusky
as a devDependencycd .. && husky install App/.husky
yarn install
As you can see, the
prepare
script is run after the yarn package is installed, but before Husky complains about the path: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.this worked for me
You can do
HUSKY=0 yourCommand
and it should work.Can’t seem to reproduce, here’s how I’m testing:
Is your
package.json
in a nested directory compared to.git
? If that’s the case, see https://typicode.github.io/husky/#/?id=custom-directoryOtherwise could you provide minimal steps for me to reproduce (or a repo)?
I’m running 7.0.4 and this issue is happening for me.
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.
Is there any updates on this matter ?
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?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
We have followed the advice to use pinst to work around this issue:
But get the error when installing the updated version:
throw new Error(".git can't be found");
I have solved the issue. Steps mentioned below,
Hi I have the same problem in v6 😕
I experienced the same issue, but was able to resolve it by converting my
postinstall
script toprepare
instead.Hi - I’m currently getting this same issue with a
prepare: husky install
npm script throwing the .git not found error when trying to runsam 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.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
I have the same problem in version ^7.0.4
Should I try this workaround
?
This is still an error in
7.x
. My.git
andpackage.json
are at the same level.Having the same issue in
7.0.2
.@dylanesque that use-case is documented on the project homepage itself. Tested & working well for me.
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
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
Having the same issue in 7.0.1
Hitting this issue in
7.0.2
@typicode having the same issue with npm 7 (npm 6 is alright)
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?