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

Does not install git hooks when using yarn #84

Closed
Jflinchum opened this issue Dec 12, 2016 · 40 comments
Closed

Does not install git hooks when using yarn #84

Jflinchum opened this issue Dec 12, 2016 · 40 comments

Comments

@Jflinchum
Copy link

When running yarn add husky --dev, the git hooks do not get set up. I have to run the install script manually, and then it works fine.

Yarn version: 0.17.9

@SxMShaDoW
Copy link

👍 on this functionality.

@typicode
Copy link
Owner

Thanks for the feedback.
Seems it does run husky's postinstall just once, except if you ignore cache by using --force.

/tmp$ rm .git/hooks/pre-commit
rm: cannot remove '.git/hooks/pre-commit': No such file or directory
/tmp$ yarn add --dev husky 
yarn add v0.17.10
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
└─ [email protected]
Done in 0.14s.
/tmp$ cat .git/hooks/pre-commit
cat: .git/hooks/pre-commit: No such file or directory
/tmp$ yarn add --dev husky --force
yarn add v0.17.10
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Rebuilding all packages...
success Saved lockfile.
success Saved 1 new dependency.
└─ [email protected]
Done in 0.75s.
/tmp$ cat .git/hooks/pre-commit
#!/bin/sh
# husky
cd .
...

@avaly
Copy link
Contributor

avaly commented Feb 15, 2017

Can this be addressed in a future version? Projects and teams using yarn should not need to manually run the --force command in order to get the functionality working.

@typicode
Copy link
Owner

Hi @avaly,

Yes it will.
I have a few ideas for this but need to do some testing.

@elektronik2k5
Copy link

I've encountered a few issues with it too :(. Had to remove the hook manually from .git/hooks/ then yarn add husky --dev --force to make it work.

@ahstro
Copy link

ahstro commented Mar 13, 2017

I'm not experiencing this. Is it fixed? If so, close? :)

@cs-miller
Copy link

still experiencing this

@erquhart
Copy link

@typicode is this still in the works?

@typicode
Copy link
Owner

typicode commented May 17, 2017

@erquhart did another test and yarn v0.24.5 seems to solve this issue

@evenfrost
Copy link

evenfrost commented May 24, 2017

Just faced the same problem with yarn v0.24.5, same with fresh yarn cache (i.e. afteryarn cache clean). Only yarn add --dev husky --force helps.

Can be reproduced on my booted.

@typicode
Copy link
Owner

Hi @evenfrost,

Here's what I did to test:

/tmp$ mkdir A
/tmp$ cd A
/tmp/A$ git init && yarn init -y && yarn add husky --dev
Initialized empty Git repository in /private/tmp/A/.git/
yarn init v0.24.5
warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
success Saved package.json
✨  Done in 0.07s.
yarn add v0.24.5
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved lockfile.
success Saved 12 new dependencies.
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
✨  Done in 0.86s.
/tmp/A$ cat .git/hooks/pre-commit
#!/bin/sh
#husky 0.13.4
# ...

/tmp/A$ cd ..
/tmp$ mkdir B
/tmp$ cd B
/tmp/B$ git init && yarn init -y && yarn add husky --dev
Initialized empty Git repository in /private/tmp/B/.git/
yarn init v0.24.5
warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
success Saved package.json
✨  Done in 0.07s.
yarn add v0.24.5
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved lockfile.
success Saved 12 new dependencies.
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
✨  Done in 0.78s.
/tmp/B$ cat .git/hooks/pre-commit
#!/bin/sh
#husky 0.13.4
# ...

I was able to make booted work with husky this way (git needs to be initialized first in the target directory):

/tmp$ mkdir booted-test
/tmp$ cd booted-test/
/tmp/booted-test$ git init
Initialized empty Git repository in /private/tmp/booted-test/.git/
/tmp/booted-test$ cd ..
/tmp$ ./node_modules/.bin/booted booted-test/
 ✔ Copying code...
 ✔ Installing packages with Yarn...
 ✔ Booted.
/tmp$ cd booted-test/
/tmp/booted-test$ cat .git/hooks/pre-commit
#!/bin/sh
#husky 0.13.3
# ...

@evenfrost
Copy link

Hmm weird, can confirm reproducing these steps manually fixed the issue for me. Will investigate my package workflow further, thanks @typicode.

@philihp
Copy link

philihp commented Sep 20, 2018

@typicode Encountering this again.

Yarn 1.9.4
npm 6.4.1
node v8.11.4
husky1.0.0-rc.14

To get around this and force it, I added to my package.json's scripts:

  "postinstall": "node ./node_modules/husky/lib/installer/bin install"

@hjemmel
Copy link

hjemmel commented Feb 13, 2019

Same problem here using 1.13.0 and latest version of husky

@eduardoarandah
Copy link

So google can find it for the next guy:

husky hooks not working manually install

Execute this in your command line:

node ./node_modules/husky/lib/installer/bin install

@Nikodermus
Copy link

Nikodermus commented Feb 21, 2020

Why is this closed? The actual issue is not solved, is still happening in:

yarn 1.22.0
node v13.7.0
husky ^4.2.3

When I try to run the above command, it fails:

node ./node_modules/husky/lib/installer/bin install
husky > Setting up git hooks
Cannot destructure property 'name' of 'which_pm_runs_1.default(...)' as it is undefined.
husky > Failed to install

Removing .git/hooks didn't work either, neither adding it as a postinstall script did

@technoknol
Copy link

For me it was git version. I've updated git version and it worked like charm. Make sure your git is > 2.13

@lucadegasperi
Copy link

Getting the same issue as @Nikodermus here's my setup:

yarn 1.22.4
node 12.16.0
husky ^4.2.3

@allenGKC
Copy link

Why is this closed? The actual issue is not solved, is still happening in:

yarn 1.22.0
node v13.7.0
husky ^4.2.3

When I try to run the above command, it fails:

node ./node_modules/husky/lib/installer/bin install
husky > Setting up git hooks
Cannot destructure property 'name' of 'which_pm_runs_1.default(...)' as it is undefined.
husky > Failed to install

Removing .git/hooks didn't work either, neither adding it as a postinstall script did

@Nikodermus Have you solved this problem?

@allenGKC
Copy link

@Nikodermus try this method #640 (comment)

@Norserium
Copy link

Installing the last Yarn version (1.22.4) and running command yarn add --dev husky --force solved this problem for me.

@cat-walk
Copy link

Why is this closed? The actual issue is not solved, is still happening in:

yarn 1.22.0
node v13.7.0
husky ^4.2.3

When I try to run the above command, it fails:

node ./node_modules/husky/lib/installer/bin install
husky > Setting up git hooks
Cannot destructure property 'name' of 'which_pm_runs_1.default(...)' as it is undefined.
husky > Failed to install

Removing .git/hooks didn't work either, neither adding it as a postinstall script did

using the version '3.1.0' solved my problem.

    "husky": "^3.1.0",

@AndreaCitrolo
Copy link

@cat-walk you are experiencing that issue because you are not running that command through yarn so the script cannot infer your package manager name, the error is not very explicative in this case. Try adding:

    "setuphusky": "node ./node_modules/husky/husky.js install"

to your package json and then run:

yarn run setuphusky

@rossthedevigner
Copy link

rossthedevigner commented Nov 17, 2020

At version 5 (husky@latest) and I don't get the manual install either when using yarn install. Only on ^4.3.1

shirakaba added a commit to shirakaba/plugin-seed that referenced this issue Nov 25, 2020
This documents how to enable git hooks after first install: `https://typicode.github.io/husky/#/?id=install`

Normally you'd simply write `husky install`, but it failed for me (see issue typicode/husky#84 (comment)), so I replaced it with the equivalent long-form invocation, `node ./node_modules/husky/lib/installer/bin install`.
NathanWalker pushed a commit to NativeScript/plugin-seed that referenced this issue Nov 25, 2020
This documents how to enable git hooks after first install: `https://typicode.github.io/husky/#/?id=install`

Normally you'd simply write `husky install`, but it failed for me (see issue typicode/husky#84 (comment)), so I replaced it with the equivalent long-form invocation, `node ./node_modules/husky/lib/installer/bin install`.
@kabaros
Copy link

kabaros commented Nov 26, 2020

for anyone having this issue with version 4 (which seems to also happen with npm 7), the solution for me was to run

INIT_CWD=.git/hooks node ./node_modules/husky/lib/installer/bin.js install

to force installing the hooks under .git/hooks

@Xunnamius
Copy link

Xunnamius commented Nov 27, 2020

Just chiming in to say I'm experiencing this as well with husky@4... on npm 7, as @kabaros alluded to. The only solution that worked for me was to upgrade to husky@5.

@matsad3547
Copy link

I'm still having this issue:

yarn 1.22.5
node v14.12.0
husky ^5.0.9

after running yarn add --dev husky --force and cat .git/hooks/pre-commit I've still got cat: .git/hooks/pre-commit: No such file or directory

I've also tried running install.js directly from the command line via node ./node_modules/husky/lib/commands/install but that didn't work either. That's how the husky package is now structured; the above command errored for me.

@chenasraf
Copy link

The hooks won't get created for me either.

node 14.15.4
yarn 1.22.10
husky 5.0.9

The hooks won't run, install says it's successful but it doesn't create any hooks.

node_modules/.bin/husky install     
husky - Git hooks installed

But when I commit, it immediately proceeds without running anything.

package.json:

  "husky": {
    "hooks": {
      "pre-commit": "echo 'RUNNING HOOK!'"
    }
  }

@johnhayesio
Copy link

Same issue I have. I have removed and re-added husky without any change. I have also run the install file directly, but this doesn't do anything either. My versions are as follows:

node 14.15.2
yarn 1.22.10
husky 5.0.9

Same issues as @chenasraf .

@chenasraf
Copy link

chenasraf commented Feb 19, 2021

Turns out husky v5 does not pull hooks from package.json anymore. You have to use husky add [hook-name] [command]

In my opinion, that's worse as it pollutes the tree with another set of plain script files, which contain the command itself, instead of abstracting the command into the runner... Also husky add doesn't always chmod the file properly, so it's more steps to make hooks work, plus they're uglier now.

@matsad3547
Copy link

@chenasraf Doh! Missed that, thanks for pointing it out.

@lauthieb
Copy link

The hooks won't get created for me either.

node 14.15.4
yarn 1.22.10
husky 5.0.9

The hooks won't run, install says it's successful but it doesn't create any hooks.

node_modules/.bin/husky install     
husky - Git hooks installed

But when I commit, it immediately proceeds without running anything.

package.json:

  "husky": {
    "hooks": {
      "pre-commit": "echo 'RUNNING HOOK!'"
    }
  }

Same for us on https://github.com/decathlon/vitamin-web

@typicode
Copy link
Owner

Hi,

husky 5 has a breaking change regarding config, .huskyrc isn’t supported anymore. See docs for install or migration steps.

@lauthieb
Copy link

@typicode indeed, thanks!
Fixed: Decathlon/vitamin-web@29e3ef7
🎉

@Noah-Krossa
Copy link

Noah-Krossa commented Feb 28, 2021

Try to execute npx husky install after install husky like dev dependencie, this command substitute default git hooks by husky hooks.

@jasan-s
Copy link

jasan-s commented Mar 20, 2021

using yarn --force also worked for me

@srgg
Copy link

srgg commented Mar 25, 2021

I've spent a lot of time trying to figure out what is wrong, but the things are that they have changed the entire approach.

For those who are still struggling with this issue, and for newcomers: read Husky 5 Release announcement especially sections "Optional install" and "Config":

With husky 4, hooks were automatically installed. With husky 5, you have the choice and it’s explicit.

Moreover, from the "Config" section:

Previously, hooks were defined in package.json or .huskyrc. Now they’re defined directly in their corresponding hook file. Overhead is reduced and scripting is easier.

dammit..

99.9999% of existing materials described husky integration become trash..

@chenasraf
Copy link

Genuinely confused about the decision to remove support for config files, you could always link to your own scripts before, but now your are much more limited...

@LuisValgoi
Copy link

LuisValgoi commented May 27, 2021

Fixed through:

  "scripts": {
    "postinstall": "node ./node_modules/husky/lib/bin install",
    "dev": "craco start",
    "build": "craco build",
    ...
  },
   ...
  "devDependencies": {
    ...
    "husky": "^6.0.0",
    ...
  },

image

image

@taoliujun
Copy link

It is not worked to me above all.
When husky > 5, just do it.

  1. yarn add -D husky
  2. add "postinstall": "husky install", into package.json scripts
  3. run yarn run postinstall once.(just run after first install husky)
  4. npx husky add .husky/pre-commit "npm run eslint"
  5. remember add .husky into git

se030 added a commit to boostcampwm-2022/web27-Wabinar that referenced this issue Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests