typicode / husky

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

Can't add a hook #1010

Closed thany closed 2 years ago

thany commented 2 years ago

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:

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.

thany commented 2 years ago

So I've manually created a file, in hopes that that's all that needs to happen. Apparantly not.

I've created .husky\pre-commit, without extension. And put npm run lint-staged in there. Nothing more.

Then, if I do any commit:

C:\workspace\my-awesome-project\Sources\Solution>git commit -m test
error: cannot spawn Sources/Solution/.husky/pre-commit: No such file or directory

Blimey, of course that doesn't exist. It's trying to go to Sources/Solution/Sources/Solution/.husky. Probably.

Now what? How do I get Husky to realize that directory might be realtive, and that a git commit could (and can) be executed from anywhere within the directory structure??

To the question "did you make the file executable?" I would answer: mate, I'm on Windows, chmod file permissions don't exist. The only type of file that is executable, is .cmd, .bat, .exe, and .com. Extensionless files are never executable. But hey, that's what the documentation appears to want me to do, and if husky add doesn't work, what other options do I have?

typicode commented 2 years ago

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:

node node_modules/.bin/husky install ...
node node_modules/.bin/husky add ...

Related: https://github.com/typicode/husky/issues/978

thany commented 2 years ago

I wonder if this shouldn't be in the documentation then...

linzb93 commented 2 years ago

@typicode you can use commander to format lib/bin.js 。commander can solve the problem when use npx husky instead of husky scripts.

InSuperposition commented 2 years ago

A teammate ran into this issue using Git for Windows (which uses bash), the pre-commit file had CRLF line-endings, once they were changed to LF, all ran perfectly.

trajano commented 2 years ago

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:

node node_modules/.bin/husky install ...
node node_modules/.bin/husky add ...

Related: #978

The workaround in git bash is

 ./node_modules/.bin/husky set .husky/pre-commit 'npx lint-staged'

Perhaps you can add this to the README.md

MarcoFriz commented 2 years ago

I got it issues:

C:\Users\user\projects\project\node_modules\.bin\husky:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^
SyntaxError: missing ) after argument list

after use

node node_modules/.bin/husky set .husky\pre-commit "npm 
test"
Plasmadog commented 2 years ago

I get the same error that MarcoFriz reported. So, the documented form of the command doesn't work, and neither does the documented "workaround". Can the file be created manually? What would that look like?

HasanMothaffar commented 2 years ago

I'm experiencing the same error. I got the same results that the original poster got.

I'm running these NPM and Node versions on a Windows 10 machine.

npm -v
6.14.15

node -v
v14.18.0

I would be glad to provide more info if it can be of any use.

EDIT: I can confirm that this workaround works for me.

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:

node node_modules/.bin/husky install ...
node node_modules/.bin/husky add ...
igor-max commented 2 years ago

Upgrade npm to version 7

Chickynuggie commented 2 years ago

I got it issues:

C:\Users\user\projects\project\node_modules\.bin\husky:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^
SyntaxError: missing ) after argument list

after use

node node_modules/.bin/husky set .husky\pre-commit "npm 
test"

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:

"scripts": {
    ...
    "prepare": "add .husky/pre-commit \"npm run lint-fix\""
}

then simply do npm run prepare like in the beginning of the installation. Hope this helps!

thany commented 2 years ago

Upgrade npm to version 7

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)

nemchik commented 2 years ago

Just some interesting info:

So just to poke a little fun:

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

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

melvingaye commented 2 years ago

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.

zhongguodong commented 2 years ago

windows10 px husky add .husky/commit-msg "npx --no -- commitlint --edit $1"试图将提交 lint 添加到我的项目中。 image

npmrun commented 2 years ago

same issue

typicode commented 2 years ago

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:

node node_modules/.bin/husky add ...
AnsgarLichter commented 2 years ago

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.

AlexZeitler commented 2 years ago

After running npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"' in the git bash in VS Code as suggested by @AnsgarLichter, I've been able o run to use the hook from the regular cmdr window, e.g. by calling git commit -m 'test'.