Skip to content

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.

Reading Time
6 min read
Updated
2026.09.16

In short

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.

Error

exit 127

Git Hook 环境错误

npx: command not found
Path
node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/usr/bin:/bin:/usr/sbin:/sbin
Notes
  • 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.

  1. Completed: 卸载 Node

    使用 brew uninstall node 卸载当前环境

  2. Completed: 安装 NVM

    安装 Node Version Manager 进行版本管理

  3. Current: 安装 Node

    使用 NVM 安装指定版本的 Node.js

Uninstall Node

卸载 Node.jsshell
brew uninstall node
卸载 npm(可选)shell
npm uninstall npm -g
验证卸载结果shell
node -v\nnpm -v

This 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

安装 NVMshell
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
验证 NVM 安装shell
nvm -v
查看可用的 Node 版本shell
nvm ls-remote
安装指定版本的 Node.jsshell
nvm install v22.16.0

All 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

  1. 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
  2. Use version management tools:
    • Java environment: Manage using SDKMAN
    • Node.js environment: Manage using NVM
    • Python environment: Manage using pyenv

💡 Best Practices

  1. Unified Environment Management: Use the same environment management tool consistently across the team
  2. Project Configuration: Add the appropriate configuration files to the project (e.g., .sdkmanrc , .nvmrc )
  3. 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.

Keep reading

2025
  1. Resolving Git Hooks Issues in Multi-JDK Environments

    Git · Development Habits · Solution

  2. Git Config Setup for Multiple Environments

    Git · Development Habits · Open Source

  3. I became a MooTool contributor today

    Contributors · GitHub · Tools