typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
32.72k stars 1.04k forks source link

husky is using some strange node version when running hooks #247

Closed bcowgill closed 6 years ago

bcowgill commented 6 years ago

First problem, husky is using npm when I'm using yarn but that's not a big problem. However it's not picking up my current version of node configured with nvm.

$ /usr/bin/nodejs --version v0.10.25 $ /usr/bin/node --version v0.10.25 $ which node /home/me/.nvm/versions/node/v6.11.4/bin/node $ node --version v6.11.4

Which is all very nice IF I run the precommit target manually:

$ yarn precommit yarn run v1.3.2 $ yarn run lint-staged $ /home/me/workspace/node_modules/.bin/lint-staged ✔ Running tasks for ./*.js Done in 2.35s.

However, when I actually git commit it get's fouled up git commit husky > npm run -s precommit (node v4.4.7) yarn run v1.3.2 warning You are using Node "4.4.7" which is not supported and may encounter bugs or unexpected behavior. Yarn supports the following semver range: "^4.8.0 || ^5.7.0 || ^6.2.2 || >=8.0.0" warning package.json: License should be a valid SPDX license expression $ /home/me/workspace/node_modules/.bin/lint-staged lint-staged requires at least version 6 of Node, please upgrade error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

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

Where in heck is it getting node 4.4.7 from?

I have it installed, but is not currenlty active: $ nvm ls v0.12.18
v4.4.7
v6.3.1
-> v6.11.4
v8.6.0
system
default -> v4.4.7 node -> stable (-> v8.6.0) (default) stable -> 8.6 (-> v8.6.0) (default) iojs -> N/A (default) lts/* -> lts/argon (-> N/A) lts/argon -> v4.8.7 (-> N/A) lts/boron -> v6.13.0 (-> N/A) lts/carbon -> v8.9.4 (-> N/A)

Relevant parts of my package.json:

"scripts": { "lint:single": "eslint --ignore-pattern '!.eslintrc.js' ", "lint:root": "yarn lint:single ..js .js --fix", "precommit": "yarn run lint-staged" }, "lint-staged": { "./*.js": [ "yarn run lint:root" ] },

"husky": "^0.14.3",
bcowgill commented 6 years ago

I added something to show which version of node it is picking up husky > (node /home/me/.nvm/versions/node/v4.4.7/bin/node)

$ cat /home/me/.nvmrc v6.11.4 $ nvm --version 0.31.4

bcowgill commented 6 years ago

AHA, you use a project local .nvmrc file which I didn't have copying my ~/.nvmrc to the project dir made it work. Looks like without the local .nvmrc when nvm.sh is sourced it reverts to the default -> node setting

Guess there's nothing to fix except maybe would be useful if the hook scripts showed you a little more about where it gets node from...

load_nvm () {
  # If nvm is not loaded, load it
  command_exists nvm || {
    export NVM_DIR=/home/me/.nvm
    [ -s "$1/nvm.sh" ] && . "$1/nvm.sh" && echo "husky > nvm.sh will use project .nvmrc or nvm default node"
  }

  # If nvm has been loaded correctly, use project .nvmrc
  command_exists nvm && [ -f .nvmrc ] && nvm use && echo "husky > nvm.sh used project .nvmrc"
}
...
echo "husky > (node `which node`)"
echo "husky > npm run -s precommit (node `node -v`)"

or perhaps just give the extra info if there is no .nvmrc in the project dir.

dmwelch commented 6 years ago

IMO, Husky should respect the engines.node version in package.json (who wants to specify the node version in separate files!?!). Use this to fix:

load_nvm () {
  ...
  # If nvm has been loaded correctly, use project .nvmrc
  command_exists nvm && [ -f .nvmrc ]  #<--- remove the "&& nvm use" bit here 
}
...
export PATH=$PATH:/usr/local/bin:/usr/local
load_nvm BREW_NVM_DIR /usr/local/opt/nvm

if [ -f package.json ]; then
    echo "Loading Node engine from package.json"
    nvm use $(node -pe 'JSON.parse(process.argv[1]).engines.node' "$(cat package.json)")  #<--- this is the secret sauce!!!
else
    nvm use
fi
...
echo
echo "> husky - npm run -s prepush"
echo "> husky - node `node -v`"
echo
dmwelch commented 6 years ago

@typicode ^^^ I'm looking at how to make a pull request with this fix, but the code is difficult to follow. Any hints for new submitters on where to start?

typicode commented 6 years ago

@bcowgill hmm if you're using a GUI and don't have a .nvmrc file, it should use the default Node version:

default -> v4.4.7

@dmwelch thanks for the help, great approach :) personally I'd rather leave this to node version managers. There's a thread in nvm discussing this https://github.com/creationix/nvm/issues/651

Also next version of husky won't come out of the box with nvm support.

tim-field commented 6 years ago

Hmm yeah also seeing this, I don't have an .nvmrc in my project. node -v gives me v9.8.0 and which node gives me /home/tim/.nvm/versions/node/v9.8.0/bin/node yet husky is finding v5.12.0 somewhere

husky > npm run -s precommit (node v5.12.0)

lint-staged requires at least version 6 of Node, please upgrade
francisbrito commented 6 years ago

I'm running into the same issue as @tim-field except that the version that husky is reporting is actually a valid one:

screen shot 2018-04-25 at 4 10 12 pm

It seems to work up to Node.js v9.11.1 (latest version of Node before yesterday's release).

budarin commented 6 years ago

Hi! I also have lint-stage error:

... husky > npm run -s precommit (node v10.0.0) lint-staged requires at least version 6 of Node, please upgrade husky > pre-commit hook failed (add --no-verify to bypass)

windows server 2008r2 node version - 10.0.0 npm version - 6.0.0

Tom-Bonnike commented 6 years ago

I managed to fix it with:

$ npm rebuild
$ rm -rf node_modules
$ npm i

I’m not sure about the order but… 😄Simply running npm rebuild didn’t fix it for me.

budarin commented 6 years ago

Thanks! It helped.

jesstelford commented 6 years ago

The fix for node v10 is to update lint-staged to ^7.0.5:

yarn add --dev lint-staged@^7.0.5

or

npm install --dev lint-staged@^7.0.5
eladmoshe commented 6 years ago

Upgrading lint-staged indeed solved the issue, thanks @jesstelford

OhMrSticky commented 6 years ago

Fix: https://github.com/okonet/lint-staged/commit/b9d84ce8e470dddbdebca0bdf56f6dde7539c4c9

francisbrito commented 6 years ago

Worked for me too. Thanks @jesstelford

chhaymenghong commented 6 years ago

@jesstelford works like a charm. Thx you

typicode commented 6 years ago

For the Node 10 message, sorry for that, there was a bug in please-upgrade-node It was fixed a few days after, more tests were added, and all versions containing the bug were deprecated. Hopefully it won't happen again.

As for nvm, next version of husky won't try to load it anymore by default, so closing this issue. Also, it won't rely on npm specifically to run hooks 👍

FDiskas commented 5 years ago

This should be reopened. Using NVM and husky throws error Husky requires Node 8.6.0, can't run Git hook. :scissors:

Node node -v returns v10.16.0

sudo node -v returns v6.12.2

chulanovskyi commented 5 years ago

okonet/lint-staged/issues/608

VolodymyrButs commented 5 years ago

yarn add --dev lint-staged

brettz9 commented 5 years ago

Upgrading lint-staged (to 7.3.0) is not getting rid of the issue for me (including after removing node_modules and package-lock.json).

brettz9 commented 5 years ago

Nevermind, it started working after I restarted my version control program (SourceTree).

azzamaurice commented 4 years ago

For anyone else still having this issue, I've solved it by setting a default node version. $ nvm alias default <version-number>

BilalCP commented 4 years ago

Still not working, I use nvm with 12.15.0 as default alias and it's not working...

node -v -> 12.15.0 -> Husky requires Node 10, can't run Git hook.

FDiskas commented 4 years ago

Just uninstall nodejs and install only with nvm

dapenguin commented 4 years ago

Was having this issue when using the Git tools in VS Code.

Setting nvm alias default <version-number> as suggested by @azzamaurice did the trick, but only after completely closing and restarting VS Code.

karlscode commented 4 years ago

I was able to install commitizen using node v13.9.0 with the following commands in the terminal: $ git init $ yarn init -y $ sed -i '6a \ \ ,"husky": {"hooks": {"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"}}' package.json $ commitizen init cz-conventional-changelog --yarn --dev --exact $ yarn add husky -D

In a single command: $ git init ; yarn init -y; sed -i '6a \ \ ,"husky": {"hooks": {"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"}}' package.json ; commitizen init cz-conventional-changelog --yarn --dev --exact ; yarn add husky -D

After all this: $ git add . $ git commit

elizavetaShcherbatykhAis commented 4 years ago

@karlscode $ yarn add husky -D helped me, thank you!

arielmontana commented 4 years ago

This error commonly happens when you have enabled the functionality to check your code before to commit. You should run the command: ng lint with this command you should see all the problems to solve. Solve it all and then try to commit again.!

aseem2625 commented 4 years ago

Same issue, OSX. I've 3 node version managed by nvm. Default was 8. I changed my default to 11. And follow following commands in this order:

nvm ls 11 rm -rf node_modules npm i // Re-installing node modules as per node 11 npm rebuild // Don't know why I had to, but found this command in README here

Works now!

JensMadsen commented 4 years ago

I was troubled by this for some time. In my case, the problem was happening in the git extension in VS Code.

I tried all the suggested solutions but no cigar... It turned out to be due to how I started Vs Code. Usually, I start vs code form the terminal, but for some unknown reason I started vs code from the Ubuntu "app launcher" (windows key -> type: "code"). When started using the app launcher, vs code picks up the system version of node (sudo node --version) and not the nvm version.

SeanMcP commented 4 years ago

I was trying to commit with VS Code's "Source Control" panel. I updated the Node version with nvm in the integrated terminal. node -v showed the correct version, but the husky still failed.

I tried everything in this thread, but none of the solutions worked.

Finally, I checked the Node version in the standard terminal and saw, to my surprise, the incorrect version. I ran nvm use 10.13.0 in the terminal, restarted VS Code, tried to commit again, and it worked! 🎉

Give that a try if everything else fails.

mozhivan commented 4 years ago

My issue was in installed together nodejs package (ubuntu) and nvm

Husky requires Node 10 (runtime: v8.10.0), can't run Git hook.

nvm use 12
node --version
v12.18.4

nodejs --version
v8.10.0

The issue was resolved by removing nodejs package and use only nvm

sudo apt-get purge --auto-remove nodejs
nanxiaobei commented 3 years ago

if with nvm, should uninstall node installed by brew.

brew uninstall node

if installed yarn, first

brew uninstall yarn

then

brew uninstall node

then use

npm install --global yarn

to reinstall yarn. https://classic.yarnpkg.com/en/docs/install#mac-stable

after all, nvm node should work with husky.

james-vsrt commented 2 years ago

For those getting this far down and pulling your hair out, try a good ol' restart after having attempted the above fixes. Some of these changes only took effect when I'd reloaded everything.

SyedTayyabUlMazhar commented 11 months ago

So I seem to have found a really good solution.

What you need to do is specify your desired node version in .nvmrc file like this:

v18.12.0

The in your husky hook file:

. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}
source ~/.nvm/nvm.sh && nvm use  <--- this will load the node version from .nvmrc
yarn typecheck
yarn eslint
yarn test
kyrregjerstad commented 11 months ago

So I seem to have found a really good solution.

What you need to do is specify your desired node version in .nvmrc file like this:

v18.12.0

The in your husky hook file:

. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}
source ~/.nvm/nvm.sh && nvm use  <--- this will load the node version from .nvmrc
yarn typecheck
yarn eslint
yarn test

Thank you, I was struggling with this today! To elaborate slightly for those who haven't used the .nvmrc feature before (like me):

To create a .nvmrc file in the root of your project and give it the node version you need

# in you project root
echo "18.16.0" > .nvmrc
lindajeen commented 2 months ago

The advice to add .nvmrc does not help in my case. My project has .nvmrc file with 20.9.0 specified already. No matter what I do, I keep getting the error:

husky > pre-commit (node v16.13.0)
→ No staged files match any configured task.
Usage Error: This tool requires a Node version compatible with >=18.12.0