如何编译无法升级版本的已构建Angular应用?
尝试编译一个已构建完成的Angular 5应用,无法升级到最新版本,但应用理应能正常编译。
package.json配置
{"name": "my name", "version": "1.0.0", "description": "what does this do?", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "^5.2.10", "@angular/common": "^5.2.10", "@angular/compiler": "^5.2.10", "@angular/compiler-cli": "^5.2.10", "@angular/core": "^5.2.10", "@angular/forms": "^5.2.10", "@angular/http": "^5.2.10", "@angular/platform-browser": "^5.2.10", "@angular/platform-browser-dynamic": "^5.2.10", "@angular/platform-server": "^5.2.10", "@angular/router": "^5.2.10", "angular2-focus": "^1.1.0", "bootstrap": "^3.3.7", "chart.js": "^2.7.2", "classlist.js": "^1.1.20150312", "core-js": "^2.4.1", "file-saver": "^1.3.3", "font-awesome": "^4.7.0", "fullcalendar": "^3.2.0", "fullcalendar-scheduler": "^1.5.1", "jquery": "^3.1.1", "moment": "^2.18.1", "primeng": "^5.2.4", "rxjs": "^5.2.0", "typescript": "^2.8.1", "web-animations-js": "^2.2.2", "zone.js": "^0.8.5" }, "devDependencies": { "@angular/cli": "^1.6.6", "@angular/compiler-cli": "^5.2.10", "@types/core-js": "^0.9.34", "@types/jasmine": "^2.5.35", "@types/jquery": "^2.0.34", "@types/lodash": "^4.14.40", "@types/moment": "^2.13.0", "@types/node": "^6.0.45", "@types/q": "0.0.32", "@types/selenium-webdriver": "^5.2.10", "codelyzer": "^4.2.1", "jasmine-core": "~2.5.2", "jasmine-spec-reporter": "~3.2.0", "karma": "~1.4.1", "karma-chrome-launcher": "~2.0.0", "karma-cli": "~1.0.1", "karma-coverage-istanbul-reporter": "^0.2.0", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "lodash": "^4.16.2", "primeng": "^4.0.1", "protractor": "~5.1.0", "ts-node": "~2.0.0", "tslint": "~5.9.1", "typescript": "^2.8.1" } }
执行ng build时的错误信息
Your global Angular CLI version (14.0.0) is greater than your local version (1.6.6). The local Angular CLI version is used.
To disable this warning use "ng config -g cli.warnings.versionMismatch false".
@angular/compiler-cli@5.2.11 requires typescript@'>=2.4.2 <2.7.0' but 2.9.2 was found instead.
Using this version can result in undefined behaviour and difficult to debug problems.Please run the following command to install a compatible version of TypeScript.
npm install typescript@'>=2.4.2 <2.7.0'To disable this warning run "ng set warnings.typescriptMismatch=false".
Date: 2022-11-23T02:55:08.478Z
Hash: 9743454dc3930ee3a8f6 Time: 4325ms chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered] chunk {main} main.bundle.js, main.bundle.js.map (main) 303 bytes [initial] [rendered] chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 323 bytes [initial] [rendered] chunk {scripts} scripts.bundle.js, scripts.bundle.js.map (scripts) 675 kB [initial] [rendered] chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 588 kB [initial] [rendered]ERROR in src/app/nets/nets-configuration/configuration-view/configuration-view.component.ts(12,23): error TS2307: Cannot find module 'primeng/table'.
已安装的Node和Angular版本

补充问题
执行npm i时出现错误,使用npm i --force会产生警告;删除node_modules文件夹重新安装后问题仍存在,且已确认node_modules中存在primeng包。
1. 修复TypeScript版本不兼容问题
Angular 5.2.x要求TypeScript版本在>=2.4.2 <2.7.0范围内,当前package.json指定的^2.8.1超出了兼容区间,实际安装的2.9.2会引发编译问题。执行以下命令安装兼容的稳定版本:
npm install typescript@2.6.2 --save-exact
2. 解决Primeng模块找不到问题
- 清理依赖冲突:package.json的
dependencies和devDependencies中同时存在不同版本的primeng(5.2.4和4.0.1),会导致依赖混乱。删除devDependencies中的primeng条目后重新安装:npm uninstall primeng --save-dev rm -rf node_modules package-lock.json npm install - 检查导入路径:查看
node_modules/primeng目录结构,确认是否存在table文件夹。Angular 5对应的Primeng 5.x中,部分版本的表格组件可能叫作datatable,若不存在table文件夹,需将组件中的导入路径改为primeng/datatable。
3. 处理Angular CLI版本 mismatch问题
全局CLI版本过高可能引发潜在兼容性问题,建议安装与本地匹配的全局版本:
npm uninstall -g @angular/cli npm install -g @angular/cli@1.6.6
4. 切换兼容的Node版本
Angular 5.x推荐使用Node 8.x LTS版本,当前Node 16.x版本过高,易引发依赖安装或编译错误。可以用nvm切换版本:
nvm install 8.17.0 nvm use 8.17.0
切换后重新安装依赖并执行编译。
内容的提问来源于stack exchange,提问作者Ali

