husky: hook fails as it assumes yarn will be available in $PATH
husky misjudges package manager & tried to find yarn while it was upgraded using npx yarn upgrade husky --latest
. I don’t keep a global yarn & use npx yarn ...
for all my yarn needs.
Possible to let the user choose package manager?
.git/hooks/husky.sh
# Run husky-run with the package manager used to install Husky
case $packageManager in
"npm") run_command npx --no-install;;
"pnpm") run_command pnpx --no-install;;
"yarn") run_command yarn run --silent;;
"*") echo "Unknown package manager: $packageManager"; exit 0;;
esac
Instead of husky inferring which package manager it should use, would it be possible to let the user configure it for husky?
Workaround
npm install -g yarn
fixes the error.
$ HUSKY_DEBUG=1 git commit # ...
husky:debug husky v4.2.1 - pre-commit
husky:debug Current working directory is /Users/detj/src/my-project
Can't find yarn in PATH: /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:...
Skipping pre-commit hook
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
OS: macOS 10.15.3
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 15
- Comments: 19 (1 by maintainers)
I was receiving
Can't find yarn in PATH
errors when Husky commit hooks run in Sourcetree on Mac. I was able to fix this with the following steps.which yarn
. This outputs something like/Users/my-user/.nvm/versions/node/v12.x.x/bin/yarn
. Grabyarn
’s parent directory, in this case/Users/my-user/.nvm/versions/node/v12.x.x/bin/
.~/.huskyrc
file (create one if you don’t have one already). Add the following to this file:There’s a similar issue when using “husky” with SourceTree. Getting an error:
This error can’t be fixed by installing yarn globally. Any idea how to fix it?
BTW - it works with earlier versions of Husky.
I had the same problem and with this configuration it was solved
There is a section on how to set PATH if you are using a version management script like nvm
https://typicode.github.io/husky/#/?id=command-not-found
OS: macOS 10.15.7 Git Client: Fork nvm: v0.39.1 node: v16.13.2 husky: v4.3.0
I use this command and everything works fine again.
https://community.atlassian.com/t5/Bitbucket-questions/SourceTree-Hook-failing-because-paths-don-t-seem-to-be-set/qaq-p/274792
Getting the same error on husky 4.2.3
Also tried Tower which is another GUI git client and same issue there. So seems like a regression since it worked out of the box with GUI git clients in previous versions.
Running
yarn
vianpx
is not supported in v4.To answer the other question, husky 4 expects that the package manager used to install is in PATH environment variable.
There’s a section about setting up PATH for yarn 1 https://classic.yarnpkg.com/en/docs/install
As to why, it’s because of this: https://yarnpkg.com/getting-started/migration#call-binaries-using-yarn-run-rather-than-node_modulesbin
As recommended, husky uses
yarn run
to executehusky-run
binary (which runs hooks) and so it needs it to be available.Hope it makes things a little clearer 😃
Husky 5 is a little more flexible in this regard but
npm
oryarn
will probably still need to be in PATH. If you’re using a GUI Git client and tools likenvm
orbrew
, there’s a special section in the docs about setting PATH with.huskyrc
.Killed 2 hours on trying to figure out what’s wrong with Husky pre-commit hook and that
Can't find yarn in PATH
error. Switched tohusky@3.1.0
, all is working there.Wonderful, thanks a lot, you saved my time.
Worth noting Tower has additional issues with hooks on OSX:
https://www.git-tower.com/help/guides/integration/environment/mac
Not their fault, but it is a PITA. I’m working on a client machine with reduced admin access because SECURITY and it’s quite a drag having to rebase on the command line.
Does Husky really need to have locally installed Node and npm/Yarn? It makes it impossible to use with Docker, where I don’t have anything in my local machine.