使用pre commit钩子再git commit时报错“husky - pre-commit hook exited with code 1 (error)”

使用husky配置git commit规范
1.我们在使用npx husky install,初始化之后,项目根目录下会出现.husky文件夹
在这里插入图片描述
2.npx husky add .husky/pre-commit 'npx lint-staged’命令是为了在git提交的时候,使用lint-staged格式化代码
我们发现在pre-commit 文件中已经自动生成如下内容:
在这里插入图片描述
但是两次执行git commit时控制台会报错

PS C:\Users\yunlu\Desktop\yuntuan\yuntuanwebsite> git commit -m "perf: 添加eslint与git commit信息检查"
file:///C:/Users/yunlu/AppData/Local/npm-cache/_npx/8facc973fbdb1091/node_modules/lint-staged/lib/index.js:112
    if (runAllError?.ctx?.errors) {
    
    
                    ^

SyntaxError: Unexpected token '.'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:140:18)
husky - pre-commit hook exited with code 1 (error)

3.所以我们直接换一个命令来执行eslint校验,更改pre-commit 文件内容最后一行为npm run lint

#!/usr/bin/env sh

. "$(dirname -- "$0")/_/husky.sh"

npm run lint

4.再次使用git commit -m ‘test: git commit时有eslint检查测试完成’ 测试,检查是否在提交的时候按照配置的eslint格式化了代码, 我们发现这个问题已经解决了欧耶!
在这里插入图片描述
不能解决插件的问题,我就把插件解决了哈哈哈哈

猜你喜欢

转载自blog.csdn.net/weixin_38318244/article/details/126184481