huskyのpre-commitが動かなかったのでなんとかした話
以下の動画でTypeScript を勉強しようと思った。 www.youtube.com
ESLintとPrettierまではうまくいったけど、huskyがうまく動かない。。。 動画でも同じように動いていなかったので、ここまではある程度想定通り。 でも、動画での対処法をやってみたけど、うまくいかなかった。。。
いろいろ調べてなんとかhuskyが使えるようになったので、その方法を紹介。
動画内の解決方法
ls -al .git/hooks
確認
動画の通り、ls -al .git/hooks
を実行したら、*.sample
しかなかった。
$ ls -al .git/hooks total 120 drwxr-xr-x 15 murae wheel 480 4 8 06:03 . drwxr-xr-x 9 murae wheel 288 4 8 06:03 .. -rwxr-xr-x 1 murae wheel 478 4 8 06:03 applypatch-msg.sample -rwxr-xr-x 1 murae wheel 896 4 8 06:03 commit-msg.sample -rwxr-xr-x 1 murae wheel 4655 4 8 06:03 fsmonitor-watchman.sample -rwxr-xr-x 1 murae wheel 189 4 8 06:03 post-update.sample -rwxr-xr-x 1 murae wheel 424 4 8 06:03 pre-applypatch.sample -rwxr-xr-x 1 murae wheel 1643 4 8 06:03 pre-commit.sample -rwxr-xr-x 1 murae wheel 416 4 8 06:03 pre-merge-commit.sample -rwxr-xr-x 1 murae wheel 1374 4 8 06:03 pre-push.sample -rwxr-xr-x 1 murae wheel 4898 4 8 06:03 pre-rebase.sample -rwxr-xr-x 1 murae wheel 544 4 8 06:03 pre-receive.sample -rwxr-xr-x 1 murae wheel 1492 4 8 06:03 prepare-commit-msg.sample -rwxr-xr-x 1 murae wheel 2783 4 8 06:03 push-to-checkout.sample -rwxr-xr-x 1 murae wheel 3650 4 8 06:03 update.sample
なんで、husky-upgrade
を実行して、もう一度ls -al .git/hooks
$ ./node_modules/.bin/husky-upgrade husky > upgrading /Library/WebServer/Documents/test/typescript/ts-basic/package.json husky field in package.json isn't empty, skipping automatic upgrade $ ls -al .git/hooks total 120 drwxr-xr-x 15 murae wheel 480 4 8 06:03 . drwxr-xr-x 9 murae wheel 288 4 8 06:03 .. -rwxr-xr-x 1 murae wheel 478 4 8 06:03 applypatch-msg.sample -rwxr-xr-x 1 murae wheel 896 4 8 06:03 commit-msg.sample -rwxr-xr-x 1 murae wheel 4655 4 8 06:03 fsmonitor-watchman.sample -rwxr-xr-x 1 murae wheel 189 4 8 06:03 post-update.sample -rwxr-xr-x 1 murae wheel 424 4 8 06:03 pre-applypatch.sample -rwxr-xr-x 1 murae wheel 1643 4 8 06:03 pre-commit.sample -rwxr-xr-x 1 murae wheel 416 4 8 06:03 pre-merge-commit.sample -rwxr-xr-x 1 murae wheel 1374 4 8 06:03 pre-push.sample -rwxr-xr-x 1 murae wheel 4898 4 8 06:03 pre-rebase.sample -rwxr-xr-x 1 murae wheel 544 4 8 06:03 pre-receive.sample -rwxr-xr-x 1 murae wheel 1492 4 8 06:03 prepare-commit-msg.sample -rwxr-xr-x 1 murae wheel 2783 4 8 06:03 push-to-checkout.sample -rwxr-xr-x 1 murae wheel 3650 4 8 06:03 update.sample
だめ。ここまで動画通り
huskyを再インストール
$ npm uninstall husky removed 13 packages, and audited 707 packages in 3s 70 packages are looking for funding run `npm fund` for details 5 high severity vulnerabilities To address all issues (including breaking changes), run: npm audit fix --force Run `npm audit` for details. $ npm install -D husky added 1 package, and audited 708 packages in 1s 71 packages are looking for funding run `npm fund` for details 5 high severity vulnerabilities To address all issues (including breaking changes), run: npm audit fix --force Run `npm audit` for details.
んでもう一回ls -al .git/hooks
$ ls -al .git/hooks total 120 drwxr-xr-x 15 murae wheel 480 4 8 06:03 . drwxr-xr-x 9 murae wheel 288 4 8 06:03 .. -rwxr-xr-x 1 murae wheel 478 4 8 06:03 applypatch-msg.sample -rwxr-xr-x 1 murae wheel 896 4 8 06:03 commit-msg.sample -rwxr-xr-x 1 murae wheel 4655 4 8 06:03 fsmonitor-watchman.sample -rwxr-xr-x 1 murae wheel 189 4 8 06:03 post-update.sample -rwxr-xr-x 1 murae wheel 424 4 8 06:03 pre-applypatch.sample -rwxr-xr-x 1 murae wheel 1643 4 8 06:03 pre-commit.sample -rwxr-xr-x 1 murae wheel 416 4 8 06:03 pre-merge-commit.sample -rwxr-xr-x 1 murae wheel 1374 4 8 06:03 pre-push.sample -rwxr-xr-x 1 murae wheel 4898 4 8 06:03 pre-rebase.sample -rwxr-xr-x 1 murae wheel 544 4 8 06:03 pre-receive.sample -rwxr-xr-x 1 murae wheel 1492 4 8 06:03 prepare-commit-msg.sample -rwxr-xr-x 1 murae wheel 2783 4 8 06:03 push-to-checkout.sample -rwxr-xr-x 1 murae wheel 3650 4 8 06:03 update.sample
・・・は?
まぁ、1年以上前の動画だから、こういうこともあるよね。 というわけでここから解決法。
huskyのバージョンを変更する
解決は結構簡単で以下のサイトにあるnpx mrm@2 lint-staged
を実行したらいい。
rinoguchi.net
$ npx mrm@2 lint-staged Running lint-staged... Update package.json husky - Git hooks installed husky - created .husky/pre-commit
あとは実際にcommitしてみる。
$ git add . $ git commit -m 'test' ✔ Preparing... ✔ Running tasks... ✔ Applying modifications... ✔ Cleaning up... ✔ Preparing... ✔ Running tasks... ✔ Applying modifications... ✔ Cleaning up... [master 70174f7] test 2 files changed, 1 insertion(+), 3 deletions(-)
動いた。
原因はnpmのバージョン?
1年前の動画だから、nodeとnpmのバージョンも違ってるだろうから、今回のバグが起こったのかも。 詳しい原因は分かってないので、わかる人がいたら教えてください!!
同じように困っている人の助けになれば幸いです。