husky: Can't add a hook
Troubleshoot
- Before creating an issue, please check: https://typicode.github.io/husky/#/?id=troubleshoot
If you’re migrating from husky 4, see: https://typicode.github.io/husky/#/?id=migrate-from-v4-to-v7
Checked that, script doesn’t work. No matter, I’ll just reconfigure manually. But therein lies the problem.
I’ve installed Husky manually, by going to the level where .git
is:
cd ..\..
npx husky install Sources/Solution/.husky
Context Just a plain old command prompt.
I execute npx husky add .husky/pre-commit "npm run lint-staged"
as per the documentation. From within the aforementioned Sources/Solution
directory. Output is as follows:
Usage:
husky install [dir] (default: .husky)
husky uninstall
husky set|add <file> [cmd]
No files are touched. How am I not doing exactly what both Husky and the documentation are expecting me to do?
I have:
- Replaced
add
withset
. - Replaced
/
with\
. - Replaced double quotes with single quotes
- Removed quotes
And all combinations of these. NONE of them work. There seems to be no way in hell or high waters to get Husky to add a hook. And since the migration script doesn’t work, it puts me in a position to downgrade back to v4, which would be sad.
I’ve noticed that my husky install
command created a subdirectory _
inside .husky
. Not sure if this is normal, but it seems weird to me. Thought I’d mention it.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 20 (2 by maintainers)
One cannot just simply upgrade to npm 7.
Some people need to stick to the previous Node.js version, for a plethora of reasons. Some use NVM with that, which forces npm v6.
Some work in teams, where you can’t just go “hey all, we need to do an upgrade that is actually unsupported on our Node.js version, because such and such package refuses to work otherwise”. And then everyone needs to manually do that, with all risks involved, which nukes any form of automated installation. And because of course not everyone will do this straight away, it will cause huge diffs in
package-lock.json
without people noticing, because github hides those.Updating npm manually? No. - I’ll stick with whichever version comes with the Node.js that happens to be used.
Long story short, it should just work in npm v6.
(But of course, the longer this issue is being dragged on without a fix, the less it will be a problem - I’m sure in 2024 everyone will be on npm v7, by which time a new Husky will be out that only works in npm v8, no doubt)
I had the same issue on a very similar win10 env, unfortunately HasanMothaffar’s workaround didn’t work for me, so how I got around it is I added the script to my package.json script:
then simply do
npm run prepare
like in the beginning of the installation. Hope this helps!Upgrade npm to version 7
The workaround in git bash is
Perhaps you can add this to the README.md
AFAICT this is an issue with npm. The same command with yarn or pnpm should work. As a temporary workaround, you can use the following from the command line:
Related: https://github.com/typicode/husky/issues/978
I got it issues:
after use
A teammate ran into this issue using Git for Windows (which uses
bash
), thepre-commit
file had CRLF line-endings, once they were changed toLF
, all ran perfectly.I wonder if this shouldn’t be in the documentation then…
Didn’t woprk for me with the latest npm and node versions. As soon as I used the git bash terminal (inside of Visual Studio Code) I could run the command without problems. Maybe this is a workaround even for older versions of npm and node.
It seems to be fixed with latest npm 8 on Windows. I haven’t tried with older versions.
You can use the following workaround if you have trouble with npx on Windows:
I also am having this issue. I am on windows and line-endings are
LF
.npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"
trying to add commit lint to my project.Just some interesting info:
So just to poke a little fun:
I would hope in 2024, everyone will be using at least nodejs 16, which ships with npm v8.
Another fun tidbit:
https://github.com/npm/documentation/issues/47
Anyway, I would agree that as of writing this comment, npm v6 probably isn’t considered EOL (I can’t find anything about npm’s lifecycle outside of the nodejs lifecycle https://nodejs.org/en/about/releases/ so I would think when nodejs ships an LTS that the included npm version would carry the same EOL date. With that said It’s reasonable to want npm v6 to be supported by the newest versions of husky, but it’s the dev’s choice if they opt to do that. There are past versions of husky that do work with npm v6, but I don’t know if there are any security concerns with any of those versions (haven’t looked).