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

Pre-commit hook can't find node #311

Closed
bmv437 opened this issue Jun 14, 2018 · 21 comments
Closed

Pre-commit hook can't find node #311

bmv437 opened this issue Jun 14, 2018 · 21 comments

Comments

@bmv437
Copy link

bmv437 commented Jun 14, 2018

Hey there,

I'm using husky to run a pre-commit hook on my repo, and everything runs correctly in my terminal when I run
git commit -m "some message"

The issue is when I try and commit with Fork, and it attempts to run the pre-commit hook and fails because it can't find node. Here's the full error from Fork

Pre-commit hook 'pre-commit' has failed:
husky > npm run -s precommit (node )


husky > pre-commit hook failed (add --no-verify to bypass)

/Users/brandon/project/.git/hooks/pre-commit: line 49: node: command not found
env: node: No such file or directory

when I run which node I get
/Users/brandon/.nvf/installed/node-8.5.0/bin/node

Full disclosure I'm using macOS, with fish shell, and nvf to manage my node versions. I just really wonder how Fork is running the pre-commit in a way where it can't find my node installation. Let me know if you have any questions or need more info.

@DanPristupov
Copy link
Contributor

It looks like Fork doesn't have node in PATH in ENV.
You can use absolute paths in your hook or you can run From from your terminal. In this way Fork will inherit PATH of the terminal and will be able to use same aliases.

@DanPristupov
Copy link
Contributor

I suppose I can close this

@nicekiwi
Copy link

This is still an issue for me. When using Node Version manager, fork can't seem to find the npm executable.

@DanPristupov
Copy link
Contributor

DanPristupov commented Mar 11, 2020

@nicekiwi did you try to start Fork from terminal as I mentioned in my comment?

Where is npm located in your system ($ which npm)?

@nicekiwi
Copy link

nicekiwi commented Mar 17, 2020

@DanPristupov yeah it does work starting form command line.

/Users/ezrasharp/.nvm/versions/node/v13.0.1/bin/npm

@marianhlavac
Copy link

You can use absolute paths in your hook or you can run From from your terminal.

Well, I understand that the problem isn't trivial, but this isn't much elegant solution for the problem.

  • Using absolute paths in hooks will break them for other developers in team.
  • Running Fork always from terminal seems to be counter-intuitive, I like to have Fork on my macOS dock or run it using Spotlight.

I'd understand this behaviour when using bundled Fork git instance, but the system git instance should be equivalent to typing git into my default shell.

@DanPristupov
Copy link
Contributor

I'd understand this behaviour when using bundled Fork git instance, but the system git instance should be equivalent to typing git into my default shell.

No, things in OS don't work like that. In *nix (including BSD, including macOS) a process inherits environment of the parent process. The environment of your shell is available only for that shell process or the processes which it started. Fork (or any other process started from Dock or Spotlight) doesn't have an access to it.

@marianhlavac
Copy link

marianhlavac commented Nov 13, 2020 via email

@McFlyssss
Copy link

I'm facing a similar issue. Fork is using a different node version than my project so then when running the tests with hooks they do not pass (They crash because of the node version).

Any suggestion guys?

@DanPristupov
Copy link
Contributor

@McFlyssss Start Fork from the terminal using open -a Fork command.

@Legys
Copy link

Legys commented Mar 8, 2021

typicode/husky#390 (comment) This worked fine for me, none of the existing solutions didn't help. They just change nothing.

@svvitale
Copy link

You can also symbolic link your npm installation to one of the directories listed in /etc/paths OR add the necessary profile paths to that file. This avoids the need to start Fork from the terminal AND avoids modifying your pre-commit hooks.

@msgirlperl
Copy link

I always start fork from my terminal and it's still using the wrong version for me. Any other ideas? I tried open -a fork (I typically just type fork) but it still didn't work.

which npm yields:
/Users/mcrowley/.nvm/versions/node/v12.14.1/bin/npm for me.

Attempting to commit through fork gives:

husky > pre-commit (node v13.7.0)
warning Integrity check: System parameters don't match
error Integrity check failed
error Found 1 errors.
husky > pre-commit hook failed (add --no-verify to bypass)

Full disclosure: I'm not super familiar with macs.

@DanPristupov
Copy link
Contributor

@msgirlperl
Try to add echo $PATH to the hook and run it from Fork. It will show what PATH is used.

@jogly
Copy link

jogly commented Aug 11, 2021

You can also symbolic link your npm installation to one of the directories listed in /etc/paths OR add the necessary profile paths to that file. This avoids the need to start Fork from the terminal AND avoids modifying your pre-commit hooks.

this worked for me on mac, for example:

ln -s $(which node) /usr/local/bin/node

@dgattey
Copy link

dgattey commented Feb 11, 2022

@jogly thanks, works perfectly and avoids having to mess with starting Fork some complicated way other than a dock icon

@certainlyakey
Copy link

certainlyakey commented Mar 22, 2023

This comment resolved the issue for me: typicode/husky#390 (comment)

If you use a node version manager, basically, create .huskyrc in your home directory, insert a code there that inits NVM/fnm/etc in your Bash or ZSH profile, then set the default node version.

For fnm it's:

eval "$(fnm env)"
fnm use default

@iethree
Copy link

iethree commented Aug 15, 2023

I had a homebrew node installation conflicting with my fnm version node manager somehow. brew uninstall node cleared it all up for me.

@shelbyspeegle
Copy link

shelbyspeegle commented Feb 14, 2024

This ended up working for me (husky git hooks):

In the repo's pre-commit file, basically initialize nvm before using node / npx:

# Ignore errors because this may fail to load in some environments.
set +e; . $HOME/.nvm/nvm.sh; set -e

@ospfranco
Copy link

Still running into this in 2024. Besides my previous answer (symbolic link to /bin folder, which by the way, also fixes some XCode errors if you are using React Native or some other framework that uses node), the solutions by hook managers like Husky or LeftHook is an rc file.

Here is the documentation for LeftHook RC File

@LMFinney
Copy link

LMFinney commented Feb 6, 2025

This comment resolved the issue for me: typicode/husky#390 (comment)

If you use a node version manager, basically, create .huskyrc in your home directory, insert a code there that inits NVM/fnm/etc in your Bash or ZSH profile, then set the default node version.

For fnm it's:

eval "$(fnm env)"
fnm use default

Thanks for this.

FYI, I got an error with husky@9.1.7 that .huskyrc has been deprecated. Following the error message, I moved the file to ~/.config/husky/init.sh, and it worked great.

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