Git Hooks and Node Environment Issues
In enterprise development, you may encounter projects that use Git hooks, including a `git pre-commit` hook. This article discusses the issues I encountered when using Git hooks.
- 6 min read
- 2026.09.16
When a Git hook reports “npx: command not found,” the root cause is usually a mismatch between the Node.js installed via Homebrew and the PATH referenced by the hook. Simply uninstall the Homebrew version of Node.js and switch to managing it with nvm; the same principle applies to using SDKMAN for Java and pyenv for Python.
Git Hooks and Node Environment Issues
🧠Cause
I previously resolved the issue described in Git Hooks and Multi-JDK Environment Issues. However, today I encountered another problem where the local Node environment 🪝 hooks were not being recognized. It really has been a series of setbacks.
Git Hook 环境错误
npx: command not found
- node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/usr/bin:/bin:/usr/sbin:/sbin
- Git hook 无法读取到 node 环境
- node_modules 文件夹中没有 npx 执行文件
- PATH 环境变量配置问题
The error message is as follows:
# 下面这一行是 sdkmanrc 的配置环境
-e Using java version 8.0.452-amzn in this shell.
# 这里开始是我的 node 环境问题
.husky/pre-commit: line 16: npx: command not found
husky - pre-commit script failed (code 127)
husky - command not found in PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/usr/bin:/bin:/usr/sbin:/sbin
Judging from the shell log above, Git hooks can no longer detect my Node environment. Additionally, the node_modules folder installed via npm install in the project does not contain the npx executable.
❌ Problem
My development computer is a MacBook Pro with an M1 Pro chip. For developers new to the Mac system, when setting up their own shell environment on a new computer, I imagine some of you will use Homebrew, just like I did. I have to admit that installing packages this way is indeed convenient—all it takes is brew install xxx. However, this can also lead to environment mismatch issues.
Homebrew
/opt/homebrew/bin/- 环境不匹配,与生产环境差异过大
- 安装方便
- 包管理统一
- 路径与标准环境不同
- 可能导致兼容性问题
- Git hooks 无法正确识别
原生环境
/usr/bin/- 生产环境标准
- 与生产环境一致
- 兼容性好
- Git hooks 正常工作
- 需要手动管理版本
- 安装相对复杂
Before writing this document, my shell environment consisted entirely of packages installed via Homebrew. Specifically, the files installed via brew install are placed in the /opt/homebrew/bin/ directory, which differs from where the native environment actually reads them. Most Linux and Unix developers use directories like /usr/bin and /usr/sbin to access the executables they need, and this differs significantly from actual production environments.
🔧 Solution
> Now that we know where the problem lies, let’s get to work on fixing it! > My issue is that brew install node prevents git hook from being accessible. > The rest of this document explains how to resolve this issue.
Completed: 卸载 Node
使用 brew uninstall node 卸载当前环境
Completed: 安装 NVM
安装 Node Version Manager 进行版本管理
Current: 安装 Node
使用 NVM 安装指定版本的 Node.js
Uninstall Node
brew uninstall nodenpm uninstall npm -gnode -v\nnpm -vThis completes the uninstallation of the Node environment installed via Homebrew. Next, we’ll install nvm
Install nvm
> Please Google “What is nvm” yourself
We’ll use the instructions provided by the nvm author on GitHub. GitHub: https://github.com/nvm-sh/nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bashnvm -vnvm ls-remotenvm install v22.16.0All set. If you’ve made it this far, you’ve successfully completed the process. You can now use it as usual.
📝 Summary
Through resolving this issue, I’ve summarized some important lessons regarding development environment management:
🔄 Choosing an Environment Management Tool
- Avoid using Homebrew to install development language environments: Although Homebrew is convenient, it can cause path inconsistencies that interfere with the proper functioning of Git hooks
- Use version management tools:
- Java environment: Manage using SDKMAN
- Node.js environment: Manage using NVM
- Python environment: Manage using pyenv
🔗 Related Articles
- Git Hooks and Multi-JDK Environment Issues - Resolving Java environment issues
- SDKMAN Installation and Configuration Guide - Detailed installation tutorial for SDKMAN
💡 Best Practices
- Unified Environment Management: Use the same environment management tool consistently across the team
- Project Configuration: Add the appropriate configuration files to the project (e.g.,
.sdkmanrc,.nvmrc) - Documentation: Document the environment setup steps in the project documentation so team members can quickly set up their environments
This way, whether the issue involves a Java or Node.js environment, it can be properly resolved, ensuring that Git hooks function correctly.