报错 | 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

相关推荐
挖掘狂人14 小时前
Git 从 0 到 1:用一个小项目走完 add / commit / reset / merge / rebase / push
git·后端·github
codigger15 小时前
Git 三区域模型:把 add、commit、reset、merge 一次讲透
git·github·编程·编程语言
水饺编程15 小时前
第1章:开发环境搭建,在 Windows 中安装 Git
linux·c语言·汇编·git·ubuntu
idanzk15 小时前
Git 推送 GitHub 报 SSL_READ /src refspec main 不匹配 完整踩坑记录
git·github·ssl
星恒随风18 小时前
Linux开发工具详解(二):Git版本控制、GitHub协作与GDB调试实战
linux·笔记·git·学习·github
寺中人1 天前
Xshell 完全入门指南:从安装到实战,远程连接+文件传输+会话管理全拆解
git·ssh·github·php·远程连接·xshell·运维工具
酬谢神明则必安2 天前
git学习记录01
linux·git·学习
lingchen19062 天前
版本控制 Git源代码项目管理
git
虫无涯2 天前
Coverity 如何结合 GJB8114-2013使用?
git·单元测试·嵌入式测试·coverity·静态扫描
喵本喵叁肆2 天前
Git 深度解读:从对象模型到分支指针,把版本控制的内核讲透
git