えぐろぐ

https://twitter.com/eggpogg

Intellij x husky x nodenv 時に pre-commit が失敗したときの対応

Intellij IDEA で日頃開発をしており、Project で npm の husky を使って、 pre-commit 時に lint チェックをしていたが、 Intellij 内の Git から commit をしたときだけ以下のエラーになりcommit が正常にできないことがあった。

※ terminal から git commit する場合は正常に動作する

12:42 Commit failed with error
  0 file committed, 8 files failed to commit: test
  error project-name@1.0.0: The engine "node" is incompatible with this module. Expected version ">=14.16.x". Got "14.8.x"
  error Commands cannot run with an incompatible environment.

node 14.8.x は mac の system node のバージョンだったので、 husky 実行時に nodenv による切り替えが正常に動作していないっぽい💦

調べると、 ~/.huskyrc に以下を記述すると nodenv で指定する node version で husky が実行されて解決した 👀

if command -v nodenv >/dev/null 2>&1; then
  eval "$(nodenv init -)"
fi

github.com