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)

Most upvoted comments

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.

  1. Go to my terminal and run which yarn. This outputs something like /Users/my-user/.nvm/versions/node/v12.x.x/bin/yarn. Grab yarn’s parent directory, in this case /Users/my-user/.nvm/versions/node/v12.x.x/bin/.
  2. Edit your ~/.huskyrc file (create one if you don’t have one already). Add the following to this file:
# Add yarn to path
export PATH=/Users/my-user/.nvm/versions/node/v12.x.x/bin:$PATH

There’s a similar issue when using “husky” with SourceTree. Getting an error:

Can't find yarn in PATH: /Library/Developer/CommandLineTools/usr/libexec/git-core:/Applications/SourceTree.app/Contents/Resources/bin:/usr/bin:/Applications/SourceTree.app/Contents/Resources/git_local/gitflow:/Applications/SourceTree.app/Contents/Resources/git_local/git-lfs:/usr/bin:/bin:/usr/sbin:/sbin
Skipping pre-commit hook

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

  1. Go to my terminal and run which yarn. This outputs something like /Users/my-user/.nvm/versions/node/v12.x.x/bin/yarn. Grab yarn’s parent directory, in this case /Users/my-user/.nvm/versions/node/v12.x.x/bin/.
  2. Edit your ~/.huskyrc file (create one if you don’t have one already). Add the following to this file:
# Add yarn to path
export PATH=/Users/my-user/.nvm/versions/node/v12.x.x/bin:$PATH

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

# ~/.huskyrc
# This loads nvm.sh and sets the correct PATH before running the hook
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

OS: macOS 10.15.7 Git Client: Fork nvm: v0.39.1 node: v16.13.2 husky: v4.3.0

sudo launchctl config user path `echo $PATH`

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

Can't find yarn in PATH

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 via npx 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 execute husky-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 or yarn will probably still need to be in PATH. If you’re using a GUI Git client and tools like nvm or brew, 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 to husky@3.1.0, all is working there.

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.

  1. Go to my terminal and run which yarn. This outputs something like /Users/my-user/.nvm/versions/node/v12.x.x/bin/yarn. Grab yarn’s parent directory, in this case /Users/my-user/.nvm/versions/node/v12.x.x/bin/.
  2. Edit your ~/.huskyrc file (create one if you don’t have one already). Add the following to this file:
# Add yarn to path
export PATH=/Users/my-user/.nvm/versions/node/v12.x.x/bin:$PATH

Wonderful, thanks a lot, you saved my time.

Getting the same error on husky 4.2.3

Can't find yarn in PATH

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.

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.