lint-staged 无法提交代码 this.options.fields ??= {};SyntaxError: Unexpected token '??=

项目重新安装依赖以后,突然无法重新提交代码了, 纳尼!!!!

基础环境

makefile 复制代码
node: 14.21.3
"lint-staged": "^13.2.2"

报错信息

shell 复制代码
 ******* pre-commit 钩子触发 **************
file:///D:/projects/carbon-credit/node_modules/listr2/dist/index.js:206
    this.options.fields ??= {};
                        ^^^                    
SyntaxError: Unexpected token '??='
at Loader.moduleStrategy (internal/modules/esm/translators.js:149:18)
husky - pre-commit hook exited with code 1 (error)

从报错结果分析, 是配置了 pre-commit 钩子,执行了什么动作, 然后提示,无法提交 commit 信息

.husky/pre-commit

bash 复制代码
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
echo ''
echo -e "\e[0;32m ******* pre-commit 钩子触发 **************\e[0m"
echo ''

npx --no-install lint-staged

这里执行了 lint-staged

分析过程

  1. 查看是哪个包依赖了 listr2, 不是直接从node_modules 里面搜搜, 看lock文件
  2. 之前是好的, 说明一定是因为某个环境的变化导致了这个问题的发生。

既然定位到了 lint-staged , 则猜测,是 lint-staged 依赖 listr2, 然后 listr2 包语法发生了变化,导致不兼容

查看 lint-staged 的 issue-1315 , 找到类似答案。 node 版本必须为 16 + 才行。

nvm 切换node 版本为 16.20.2

再次提交代码 git commit -m 'feat: 初始化项目'

可以了~

解决办法

  • 方式1: node 升级到16
  • 方式2: 公司业务环境限制,无法升级到node 16+的, 降级lint-staged 从 13.2.213.2.0
相关推荐
尾善爱看海18 小时前
不常用的浏览器 API —— Web Speech
前端
美酒没故事°19 小时前
vue3拖拽+粘贴的综合上传器
前端·javascript·typescript
jingling55520 小时前
css进阶 | 实现罐子中的水流搅拌效果
前端·css
悟能不能悟21 小时前
前端上载文件时,上载多个文件,但是一个一个调用接口,怎么实现
前端
可问春风_ren1 天前
前端文件上传详细解析
前端·ecmascript·reactjs·js
羊小猪~~1 天前
【QT】--文件操作
前端·数据库·c++·后端·qt·qt6.3
晚风资源组1 天前
CSS文字和图片在容器内垂直居中的简单方法
前端·css·css3
Miketutu1 天前
Flutter学习 - 组件通信与网络请求Dio
开发语言·前端·javascript
光影少年1 天前
前端如何调用gpu渲染,提升gpu渲染
前端·aigc·web·ai编程
Surplusx1 天前
运用VS Code前端开发工具完成网页头部导航栏
前端·html