Angular 8项目运行报TypeError及ng命令执行失败求助
Angular 8.x 升级VSCode后运行报错及CLI命令失效解决方案
问题1:VSCode升级至1.68.1后项目运行异常
- 环境:Node 16.13.0、Angular 8.3.1
- 核心报错:
core.js:23018 Uncaught TypeError: Cannot read properties of undefined (reading 'id') - 关联模块:调试指向
LoadingBarHttpClientModule、LoadingBarRouterModule,移除后报错转移至NgxsRootModule,错误触发点在core.js的registerNgModuleType函数中判断ngModuleType.ngModuleDef.id处 - 已尝试无效操作:禁用Angular Language Service、删除/重建
node_modules、执行npm install/npm update
问题2:ng new等CLI命令无法执行
- 报错表现:弹出Windows Script Host提示,内容为
\AppData\Roaming\npm\node_modules@aungular\cli\bin\ng.js第1行第1字符无效(注:路径中@aungular为拼写错误,正确应为@angular)
问题1解决步骤
锁定Angular Language Service版本
VSCode 1.68.1内置的语言服务版本与Angular 8.x不兼容,手动安装项目适配版本:npm install @angular/language-service@8.3.1 --save-dev配置VSCode强制使用本地语言服务:
- 打开设置(快捷键
Ctrl+,) - 关闭
angular.enable-experimental-ivy-language-service - 开启
angular.use-local-language-service
- 打开设置(快捷键
重装兼容版本的依赖模块
LoadingBar和NGXS模块的当前版本可能与Angular 8.x存在适配问题,安装指定兼容版本:- 安装适配Angular 8的LoadingBar:
npm install ngx-loading-bar@4.2.0 --save - 安装适配Angular 8的NGXS:
npm install @ngxs/store@3.7.4 --save
清理缓存并重启项目:
npm cache clean --force ng serve --open- 安装适配Angular 8的LoadingBar:
禁用Ivy编译引擎
在tsconfig.json中添加Angular编译配置,强制使用旧版编译逻辑:{ "angularCompilerOptions": { "enableIvy": false, "fullTemplateTypeCheck": true, "strictInjectionParameters": true } }
问题2解决步骤
删除错误拼写的CLI文件夹
导航到C:\Users\[你的用户名]\AppData\Roaming\npm\node_modules,删除名为@aungular的错误文件夹。全局重装适配版本的Angular CLI
npm uninstall -g @angular/cli npm cache clean --force npm install -g @angular/cli@8.3.29验证环境变量配置
确保npm全局路径C:\Users\[你的用户名]\AppData\Roaming\npm已添加到系统环境变量PATH中,重启命令提示符后测试ng new命令。
内容的提问来源于stack exchange,提问作者MichaelEvanchik
相关产品推荐
相关产品推荐

