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

hook fails as it assumes yarn will be available in $PATH #666

Closed
detj opened this issue Feb 6, 2020 · 19 comments
Closed

hook fails as it assumes yarn will be available in $PATH #666

detj opened this issue Feb 6, 2020 · 19 comments

Comments

@detj
Copy link

detj commented Feb 6, 2020

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

@detj
Copy link
Author

detj commented Mar 18, 2020

would you consider this an issue? or it is pretty much expected to have yarn available in $PATH.

@barakyosi
Copy link

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.

@axelfriberg
Copy link

axelfriberg commented Apr 2, 2020

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.

@lewislbr
Copy link

lewislbr commented Apr 5, 2020

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.

@hcharley
Copy link

hcharley commented Apr 8, 2020

Hmm, I just started running into this on VSCode.

@xarvh
Copy link

xarvh commented May 17, 2020

Having the same problem.
In Ubuntu, yarn is available as a package that provides a yarnpkg binary, that I alias as yarn.
Therefore, there is no yarn binary available strictly speaking.

(Why does husky need to know where the binaries are anyway? o_O)

@bobrosoft
Copy link

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.

@armspkt
Copy link

armspkt commented Jun 30, 2020

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

@j3ko
Copy link

j3ko commented Aug 22, 2020

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 don't think downgrading solves the underlying issue. I downgraded to 3.1.0 and it also skips the hook with this message instead (I am using nvm, not sure if that matters):

Info: can't find node in PATH, trying to find a node binary on your system
husky > pre-commit (node v10.15.1)
ℹ No staged files match any configured task.
Info: can't find node in PATH, trying to find a node binary on your system
Info: can't find node in PATH, trying to find a node binary on your system
Info: can't find node in PATH, trying to find a node binary on your system

both versions work fine on windows.

@elalemanyo
Copy link

@armspkt I try your command and nothing change... still work for you? What is your OSX version? I am using 10.15.7

@tcastelly
Copy link

I had the same problem. I'm not sure it's a nice fix, but it works for me.
Yarn is installed for alternative os

First $PATH has to be updated:

export PATH="$HOME/.yarn/bin:$PATH"

Next I add a symbol link like this:

ln -s ~/.yarn/bin/yarn.js ~/.yarn/bin/yarn

I hope it can help

@spasquali-ww
Copy link

Just for clarity, must yarn exist in $PATH for husky to function?

@typicode
Copy link
Owner

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.

@jasonworden
Copy link

jasonworden commented Oct 15, 2021

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

@luisbello30
Copy link

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

@DisasterMan78
Copy link

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.

@joeyfigaro
Copy link

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.

This issue also affects the Github desktop client, FWIW.

@SercanSercan
Copy link

If you are also desperately trying to fix ".husky/pre-commit: line 4: lint-staged: command not found", add "npx lint-staged" to your pre-commit file. Read more about the solution here: https://storksnestblog.wordpress.com/2023/01/11/fixing-husky-lint-staged-command-not-found-error/

@arthur-esterox
Copy link

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.

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