Angular 11应用执行ng build时缺失Iterable类型定义如何解决
Angular 11项目TypeScript编译异常问题记录
我是Angular与Node生态的新手,该问题可能源于基础配置疏漏。
问题现象
我在Visual Studio 2019中维护一个配置为使用TypeScript 4的Angular 11项目:
- 直接通过Visual Studio运行项目时会返回404页面,提示“Cannot GET /”
- 通过命令行执行
ng build构建时会抛出大量错误,核心为无法识别Iterable、Set、Map等类型,典型报错如下:Error: node_modules/rxjs/internal/types.d.ts:41:84 - error TS2304: Cannot find name 'Iterable'. - 即便已将tsconfig的lib编译选项设置为es2017(尝试更换为es2015仍会触发相同报错),仍会反复出现提示:“Do you need to change your target library? Try changing the
libcompiler option to es2015 or later.” - 我曾在
node_modules/@types目录下全局检索“Iterable”的定义,未找到对应声明,不确定是否缺失相关类型包。
排查进展
经初步定位,该问题疑似由某服务文件顶部手动添加的类型引用触发,对应代码为:
///<reference path="../../../../node_modules/typescript/lib/lib.dom.d.ts" /> ///<reference path="../../../../node_modules/typescript/lib/lib.d.ts" />
- 删除上述引用后项目可正常构建,但会在Visual Studio中出现大量缺失定义的错误高亮,开发体验不佳
- 若在tsconfig.json中添加
"skipLibCheck": true可临时完成构建,但并非长期适用的解决方案
已完成的排查操作
- 已更新Node.js、NPM、Angular CLI及项目内所有依赖包
- 已确认package.json中包含最新版本的@types/node
- 已核对tsconfig.json配置:target设置为es2015,lib编译选项包含"es2017"与"dom"
- 已确认构建流程实际使用上述tsconfig文件(向文件内插入无效内容可被构建流程识别报错)
- 已删除node_modules文件夹与package-lock.json文件,重新执行
npm install安装全部依赖,问题仍未解决
项目相关配置内容
tsconfig.json 配置内容
{ "compileOnSave": false, "compilerOptions": { "incremental": true, "baseUrl": "./", "module": "es2020", "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "target": "es2015", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2017", "dom" ] } }
package.json 配置内容
{ "name": "myApp", "version": "0.0.0", "scripts": { "ng": "ng", "start": "echo fix && ng serve", "build": "ng build", "build:ssr": "ng run MyApp:server:dev", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "postinstall": "ngcc" }, "private": true, "dependencies": { "@angular/animations": "11.0.6", "@angular/common": "11.0.6", "@angular/compiler": "11.0.6", "@angular/core": "11.0.6", "@angular/forms": "11.0.6", "@angular/platform-browser": "11.0.6", "@angular/platform-browser-dynamic": "11.0.6", "@angular/platform-server": "11.0.6", "@angular/router": "11.0.6", "@fortawesome/angular-fontawesome": "^0.7.0", "@fortawesome/fontawesome-free": "^5.15.1", "@fortawesome/fontawesome-svg-core": "^1.2.32", "@fortawesome/free-solid-svg-icons": "^5.15.1", "@popperjs/core": "^2.6.0", "angularx-flatpickr": "^6.5.1", "aspnet-prerendering": "^3.0.1", "core-js": "^3.8.2", "flatpickr": "^4.6.9", "jquery": "^3.5.1", "moment": "^2.29.1", "normalize.css": "8.0.1", "oidc-client": "^1.9.1", "rxjs": "^6.6.3", "toastr": "^2.1.4", "tslib": "^2.0.3", "zone.js": "~0.10.2" }, "devDependencies": { "@angular-devkit/build-angular": "^0.1100.5", "@angular/cli": "11.0.6", "@angular/compiler-cli": "11.0.6", "@angular/language-service": "11.0.6", "@types/jasmine": "^3.6.2", "@types/jasminewd2": "~2.0.8", "@types/node": "^14.14.20", "codelyzer": "^6.0.1", "jasmine-core": "~3.5.0", "jasmine-spec-reporter": "~5.0.0", "karma": "~5.1.1", "karma-chrome-launcher": "~3.1.0", "karma-coverage-istanbul-reporter": "~3.0.2", "karma-jasmine": "~3.3.0", "karma-jasmine-html-reporter": "^1.5.0", "typescript": "4.0.5" }, "optionalDependencies": { "protractor": "~7.0.0", "ts-node": "~8.4.1", "tslint": "~6.1.0" } }
angular.json 配置内容
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "MyApp": { "root": "", "sourceRoot": "src", "projectType": "application", "prefix": "app", "schematics": {}, "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "aot": true, "progress": false, "outputPath": "dist", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.app.json", "assets": [ "src/assets" ], "styles": [ "node_modules/normalize.css/normalize.css", "src/styles.scss", "node_modules/toastr/toastr.scss" ], "scripts": [ "node_modules/jquery/dist/jquery.min.js", "node_modules/toastr/toastr.js", "node_modules/moment/moment.js" ] }, "configurations": { "production": { "budgets": [ { "type": "anyComponentStyle", "maximumWarning": "6kb" } ], "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true } } }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "browserTarget": "MyApp:build" }, "configurations": { "production": { "browserTarget": "MyApp:build:production" } } }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "MyApp:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.spec.json", "karmaConfig": "src/karma.conf.js", "styles": [ "src/styles.scss" ], "scripts": [], "assets": [ "src/assets" ] } }, "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": [ "src/tsconfig.app.json", "src/tsconfig.spec.json" ], "exclude": [ "**/node_modules/**" ] } }, "server": { "builder": "@angular-devkit/build-angular:server", "options": { "outputPath": "dist-server", "main": "src/main.ts", "tsConfig": "src/tsconfig.server.json" }, "configurations": { "dev": { "optimization": true, "outputHashing": "all", "sourceMap": false, "namedChunks": false, "extractLicenses": true }, "production": { "optimization": true, "outputHashing": "all", "sourceMap": false, "namedChunks": false, "extractLicenses": true } } } } }, "MyApp-e2e": { "root": "e2e/", "projectType": "application", "architect": { "e2e": { "builder": "@angular-devkit/build-angular:protractor", "options": { "protractorConfig": "e2e/protractor.conf.js", "devServerTarget": "MyApp:serve" } }, "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": "e2e/tsconfig.e2e.json", "exclude": [ "**/node_modules/**" ] } } } } }, "defaultProject": "MyApp", "schematics": { "@schematics/angular:component": { "style": "scss" } } }
内容的提问来源于stack exchange,提问作者Alfie Woodland
相关产品推荐
相关产品推荐

