报错 | git 提交时,报错 .husky/commit-msg: line 4: npx: command not found | 仓库提交规范问题

报错:.husky/commit-msg: line 4: npx: command not found

背景:使用 nvm 管理 node 版本时,用 git bash 进行 git commit -m ''代码提交时,出现 husky 报错


本质cmd 能找到 node/npm/npx/nvm(nvm-windows) ,但在 Git Bash 看不到 ,所以 Git Bash 的****PATH 没把 【 NVM for Windows 的 node 目录 】包含进来

另外 nvm(nvm-windows)在 Git Bash 里也不是 bash 的函数/命令,所以会提示 command not found


解决:

① 找到 nvm 的路径:

在 cmd 中依次执行:

查看 npm 真实路径

where npm

查看 npx 真实路径

where npx

查看 node 的版本目录

nvm list

你应该能看到类似:

C:\Program Files\nodejs\npm C:\Users\14014\AppData\Roaming\nvm\22.12.0\npm

如果 npx 没出现,那说明 npx 没被正确安装(Node 18+ 默认去掉 npx)。

:输入 where npm ,我的输出是D:\Coding_tools\nvm\nodejs\npm

那么我的 nvm 的 地址是D:\Coding_tools\nvm\nodejs

即 path 地址是**/d/Coding_tools/nvm/nodejs:$PATH**


原因

  • 你用的是 nvm-windows (Windows 版 NVM),它把 node 放在 D:\Coding_tools\nvm\nodejs\...,这个路径已在 Windows 命令行的 PATH 中,但 Git Bash 的 PATH 没包含这个目录;

  • nvm-windows 并不会自动在 Git Bash 中暴露 nvm shell 函数,所以 nvm 在 Git Bash 上不可用(除非安装 nvm(unix 版))。


② 把 nodejs 的安装目录加入 git bash 的 PATH 中

在 git bash 中未能找到路径,但在 cmd 中输入上述 可以正确显示路径,则在git bash 中执行(一行一条):

bash 复制代码
# 1) 备份现有 bashrc(保险起见)
cp ~/.bashrc ~/.bashrc.backup

# 2) 把 nodejs 安装目录加入 PATH(追加到 ~/.bashrc)
# 【 PATH 是 你在 cmd 中用 where npm 输出的地址 去除 npm 的部分】+ $PATH
# 注意:这里斜杠的方向,和 cmd 的斜杠方向相反。
echo 'export PATH="/d/Coding_tools/nvm/nodejs:$PATH"' >> ~/.bashrc

# 3) 立即生效
source ~/.bashrc

# 4) 验证
which npm
which npx
command -v node
node -v

其他------where 和 which的区别

where(Windows 命令)

which(git bash)


在 Windows cmd 里你用 where(Windows 命令)能找到 D:\Coding_tools\nvm\nodejs\npm


仓库提交规范问题:

问题 :**Git 提交被 Husky 的 commit-msg 钩子(配合 commitlint)**拦下来了

husky - commit-msg hook exited with code 1:表示 hook 检测不通过,退出码为 1,所以 Git 阻止了提交。
提示:

✖ subject may not be empty [subject-empty]

✖ type may not be empty [type-empty]

分析 :仓库要求 Conventional Commits(规范化提交信息) 的格式,提交信息必须包含 type(例如 feat/fix/chore 等)和 subject(简短描述)。

查看项目中的 husky/commit-msg 文件中,commitlint 要求规范提交信息

导致,你写了 git commit -m '添加xxxx',commitlint 也会判断缺少 type: 前缀而拒绝。


解决:使用正确的提交信息格式:

仓库常见的格式示例(最常见):

<type>(<scope>): <subject>

中文也可以,只要有 type:subject

常用 type

  • feat --- 新功能

  • fix --- 修复 bug

  • chore --- 构建/工具/杂项

  • docs --- 文档

  • refactor --- 重构

  • test --- 测试

  • style --- 格式(不影响功能)

示例命令(直接复制使用):

git add .

git commit -m "feat: 添加 xxxx库"

或者带 scope(更严谨):

git commit -m "feat(ui): 添加 xxxx库"

如果这是修复某事改动,应使用 fix:

git commit -m "fix: 修复 添加 xxxx 未引入的问题"

例:按照规范写,成功!


若要临时跳过钩子(不推荐)------ 添加--no-verify可以跳过验证

git commit -m "添加element-plus组件" --no-verify

注意:--no-verify 会跳过所有 pre-commit / commit-msg 钩子,可能会把不合规的提交推到仓库,应该谨慎使用。



参考:visual studio code - VSCODE & GitHub Desktop pre-commit hook: npx: command not found - Stack Overflow

git提交时报错 npx: command not found问题: git提交时报错 npx: command not - 掘金安装了node新版本 使用sourcetree提交代码报错_husky - pre-commit hook exited with code 127 (erro-CSDN博客

How To | Husky

相关推荐
stevenzqzq5 分钟前
androidstudio 上配置git
git
向日葵.36 分钟前
linux & qnx & git 命令 2
linux·运维·git
恋喵大鲤鱼1 小时前
git log
git·git log
阿明62 小时前
Git的原理与使用
git
阿旭超级学得完2 小时前
Linux基础指令 四(apt,vim,git,cgdb)
linux·服务器·开发语言·数据结构·c++·git·vim
恋喵大鲤鱼15 小时前
git mv
git·git mv
Patrick_Wilson19 小时前
Git Worktree 原理详解:从 objects / refs 看懂多分支并行与多 Agent 协作
git·面试·ai编程
yaoxiaoganggang19 小时前
克隆 Superpowers 的规则库到你的本地(或者直接作为 Git Submodule)
人工智能·经验分享·git·ai编程
Python私教1 天前
用 Claude Code 做大型重构不翻车:分批+Git 兜底+验证闭环的实战流程(2026)
git·重构·ai编程·代码重构·工程实践·claude code
Shawn Dev1 天前
团队协作中的 Git Tag 最佳实践:从入门到精通
大数据·git·elasticsearch