NX Monorepo执行ng lint报错:TypeError: context.hasher.hashTask is not a function
问题:nx lint 抛出 TypeError: context.hasher.hashTask is not a function
环境背景
- 运行3年的NX Monorepo,包含Angular 13和NestJS
- 执行
nx g @nrwl/angular:application fuzzy创建新应用后,运行ng lint触发错误
错误栈
> ng lint Unexpected error: TypeError: context.hasher.hashTask is not a function at A:\web-app\node_modules\@nrwl\linter\src\executors\eslint\hasher.js:6:42 at Generator.next (<anonymous>) at A:\web-app\node_modules\tslib\tslib.js:118:75 at new Promise (<anonymous>) at Object.__awaiter (A:\web-app\node_modules\tslib\tslib.js:114:16) at run (A:\web-app\node_modules\@nrwl\linter\src\executors\eslint\hasher.js:5:20) at TasksSchedule.<anonymous> (A:\web-app\node_modules\nx\src\tasks-runner\tasks-schedule.js:117:19) at Generator.next (<anonymous>) at A:\web-app\node_modules\tslib\tslib.js:118:75 at new Promise (<anonymous>)
解决步骤
1. 统一NX生态依赖版本
这个错误核心是**@nrwl/linter版本与NX核心版本不兼容**——新增应用时自动安装的@nrwl/linter版本和现有Monorepo的NX版本不匹配,导致hashTask方法不存在。
执行命令统一所有NX相关依赖到当前Monorepo的稳定版本:
# 先查看当前NX版本 npm list nx # 替换<你的NX版本>为上面输出的版本号 npm install @nrwl/workspace@<你的NX版本> @nrwl/angular@<你的NX版本> @nrwl/linter@<你的NX版本> @nrwl/nest@<你的NX版本> --save-dev
2. 清理缓存并重装依赖
缓存残留的旧文件可能导致问题,执行:
# 重置NX缓存 nx reset # 清理npm缓存 npm cache clean --force # 重新安装依赖 npm install
3. 校验ESLint配置一致性
检查新创建的fuzzy应用根目录下的.eslintrc.json,确保其extends的配置与Monorepo中其他应用一致,避免配置冲突。
4. 降级@nrwl/linter(备选)
如果统一版本后仍报错,尝试将@nrwl/linter降级到与NX核心版本明确兼容的旧版本,比如查看package-lock.json中历史兼容的版本号,执行:
npm install @nrwl/linter@<兼容版本号> --save-dev
内容的提问来源于stack exchange,提问作者Tobias J.
相关产品推荐
相关产品推荐

