升级@nrwl/angular后用NX生成Angular库时报SchematicsException错误
NX 11.5.2 版本生成Angular库的报错排查与修复
场景背景
- 项目为monorepo架构,使用Angular v11.2.6
- NX工作区对应依赖版本为@nrwl/angular 11.5.2
- 目标为通过官方schematics生成指定路径下的Angular库
第一类报错:项目名格式校验失败
最初执行的生成命令:ng generate @nrwl/angular:library --name=service --style=scss --directory=/libs/booking
控制台返回报错:SchematicsException [Error]: Project name "-libs-booking-service" is not valid. New project names must start with a letter, and must contain only alphanumeric characters or dashes. When adding a dash the segment after the dash must also start with a letter.
- 触发原因:schematics拼接项目名时,会将带开头斜杠的directory路径转换为短横线分隔格式,最终生成的项目名以
-开头,不符合命名校验规则 - 已验证无效的操作:删除node_modules目录后重新安装全部依赖,问题无改善
调整路径后触发的第二类报错:tsconfig配置读取失败
调整directory参数、去掉开头斜杠后,执行的新命令:ng generate @nrwl/angular:library --name=services --style=scss --directory=libs/booking
控制台返回新报错:TypeError: Cannot read property 'paths' of undefined
- 触发原因:@nrwl/angular 11.5.2版本的schematics默认识别
tsconfig.base.json作为TS基础配置文件,不会读取项目原本使用的tsconfig.json,找不到对应配置文件时会触发paths属性读取的未定义错误 - 修复方案:将项目当前使用的
tsconfig.json重命名为tsconfig.base.json后,再执行生成命令即可正常运行
内容的提问来源于stack exchange,提问作者Francesco
相关产品推荐
相关产品推荐

