Angular 14安装apollo-angular遇依赖冲突问题求助
问题:Angular 14 安装 apollo-angular 依赖冲突失败
近两日尝试在Angular 14项目中安装apollo-angular,执行安装命令后出现以下错误:
The package apollo-angular@3.0.1 will be installed and executed. Would you like to proceed? Yes npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: frontend@0.0.0 npm ERR! Found: @angular/core@14.0.6 npm ERR! node_modules/@angular/core npm ERR! @angular/core@"^14.0.0" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer @angular/core@"^12.0.0 || ^13.0.0" from apollo-angular@3.0.1 npm ERR! node_modules/apollo-angular npm ERR! apollo-angular@"3.0.1" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! See C:\Users\xxx\AppData\Local\npm-cache\eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\xxx\AppData\Local\npm-cache\_logs\2022-07-18T14_07_12_565Z-debug-0.log × Packages installation failed, see above.
尝试在安装命令后添加--force或--legacy-peer-deps参数,问题仍未解决。
项目配置
angular.json
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "frontend": { "projectType": "application", "schematics": {}, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/frontend", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css", "src/styles.css" ], "scripts": [] }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "500kb", "maximumError": "1mb" }, { "type": "anyComponentStyle", "maximumWarning": "2kb", "maximumError": "4kb" } ], "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "outputHashing": "all" }, "development": { "buildOptimizer": false, "optimization": false, "vendorChunk": true, "extractLicenses": false, "sourceMap": true, "namedChunks": true } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "browserTarget": "frontend:build:production" }, "development": { "browserTarget": "frontend:build:development" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "frontend:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.spec.json", "karmaConfig": "karma.conf.js", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css", "src/styles.css" ], "scripts": [ ] } } } } }, "cli": { "analytics": false } }
package.json
{ "name": "frontend", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "watch": "ng build --watch --configuration development", "test": "ng test", "json-run": "json-server --watch db.js", "gql-server": "json-graphql-server gql.js" }, "private": true, "dependencies": { "@angular/animations": "^14.0.0", "@angular/cdk": "^14.0.4", "@angular/common": "^14.0.0", "@angular/compiler": "^14.0.0", "@angular/core": "^14.0.0", "@angular/forms": "^14.0.0", "@angular/material": "^14.0.4", "@angular/platform-browser": "^14.0.0", "@angular/platform-browser-dynamic": "^14.0.0", "@angular/router": "^14.0.0", "@ngrx/data": "^14.0.2", "@ngrx/effects": "^14.0.2", "@ngrx/entity": "^14.0.2", "@ngrx/store": "^14.0.2", "cors": "^2.8.5", "express": "^4.18.1", "express-graphql": "^0.12.0", "graphql": "^16.5.0", "jalali-moment": "^3.3.11", "rxjs": "~7.5.0", "tslib": "^2.3.0", "zone.js": "~0.11.4" }, "devDependencies": { "@angular-devkit/build-angular": "^14.0.1", "@angular/cli": "~14.0.1", "@angular/compiler-cli": "^14.0.0", "@types/jasmine": "~4.0.0", "bootstrap": "~4.3.1", "jasmine-core": "~4.1.0", "jquery": "^3.6.0", "karma": "~6.3.0", "karma-chrome-launcher": "~3.1.0", "karma-coverage": "~2.2.0", "karma-jasmine": "~5.0.0", "karma-jasmine-html-reporter": "~1.7.0", "typescript": "~4.7.2" }, "description": "This project was generated with Angular CLI version 14.0.1.", "main": "gql.js", "keywords": [], "author": "", "license": "ISC" }
解决方案
问题根源
apollo-angular@3.0.1的peer依赖仅支持Angular 12.x或13.x,与项目中的Angular 14.x版本不兼容,导致npm无法解析依赖树。即使添加--force或--legacy-peer-deps,也会因为底层版本不匹配引发潜在运行错误,因此无法解决问题。
解决步骤
安装适配Angular 14的apollo-angular版本,即4.x及以上版本,执行以下命令:
npm install apollo-angular@^4.0.0
额外说明
- apollo-angular从4.0.0版本开始正式支持Angular 14,安装该版本后npm会自动匹配兼容的peer依赖。
- 若需要安装apollo生态的其他依赖(如apollo-client、graphql等),建议保持版本与apollo-angular同步,避免再次出现依赖冲突。
- 安装完成后,可运行
ng build或ng serve验证项目是否能正常构建和运行。
内容的提问来源于stack exchange,提问作者DolDurma
相关产品推荐
相关产品推荐

