You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular项目升级Lodash至4.17.21失败,请求技术协助

升级Angular项目中Lodash至4.17.21版本失败的解决方法

我需要将Angular项目中的Lodash从4.17.20版本升级至4.17.21版本,但执行npm update lodash命令时出现错误。

依赖树信息

运行npm ls lodash得到以下依赖树:

@angular-devkit/build-angular@13.2.5
| +-- inquirer@8.2.0
| | `-- lodash@4.17.21
| `-- webpack-dev-server@4.7.3
|   `-- portfinder@1.0.28
|     `-- async@2.6.3
|       `-- lodash@4.17.20 deduped
+-- @angular/localize@13.2.4
| `-- @babel/core@7.8.6
|   `-- lodash@4.17.20 deduped
+-- karma@6.3.4
| `-- lodash@4.17.21
+-- ngx-treeview@10.0.2
| `-- lodash@4.17.20
`-- node-sass@5.0.0
  +-- gaze@1.1.3
  | `-- globule@1.3.2
  |   `-- lodash@4.17.20 deduped
  +-- lodash@4.17.20 deduped
  `-- sass-graph@2.2.5
    `-- lodash@4.17.20 deduped

项目package.json内容

{
    "name": "client-app",
    "version": "0.0.0",
    "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e",
        "build_lib": "ng build capture-plugin",
        "npm_pack": "cd dist/capture-plugin && npm pack",
        "copy-npmrc": "copy .\\projects\\capture-plugin\\.npmrc .\\dist\\capture-plugin",
        "copy-files": "npm run copy-npmrc",
        "package": "npm run build_lib && npm run copy-files && npm run npm_pack",
        "publish": "npm run build_lib && npm run copy-files && npm publish"
    },
    "private": true,
    "comments": {
        "forlocal": "ng build --configuration=local",
        "fordev": "ng build --configuration=dev",
        "forProd": "ng build --configuration=production"
    },
    "dependencies": {
        "@angular/animations": "^13.2.4",
        "@angular/cdk": "^11.2.13",
        "@angular/common": "13.2.4",
        "@angular/compiler": "13.2.4",
        "@angular/core": "13.2.4",
        "@angular/forms": "13.2.4",
        "@angular/localize": "13.2.4",
        "@angular/material": "^11.2.13",
        "@angular/platform-browser": "13.2.4",
        "@angular/platform-browser-dynamic": "13.2.4",
        "@angular/platform-server": "13.2.4",
        "@angular/router": "13.2.4",
        "@azure/msal-angular": "^2.1.1",
        "@azure/msal-browser": "^2.22.0",
        "@ng-bootstrap/ng-bootstrap": "^9.1.3",
        "@ng-select/ng-select": "^8.1.1",
        "angular-bootstrap-md": "^12.0.0",
        "angular-moment": "^1.3.0",
        "angular-ng-autocomplete": "^2.0.5",
        "angular2-multiselect-dropdown": "^5.0.4",
        "angular2-uuid": "^1.1.1",
        "animate.css": "^4.1.1",
        "arcfooter": "file:arcfooter-0.0.9.tgz",
        "bn-ng-idle": "^1.0.1",
        "bootstrap": "^4.6.0",
        "core-js": "^3.16.3",
        "crypto-js": "^4.0.0",
        "d3-tip": "^0.9.1",
        "d3v4": "^4.2.2",
        "file-saver": "^2.0.5",
        "jquery": "^3.6.4",
        "microsoft-adal-angular6": "^1.3.0",
        "moment": "^2.29.1",
        "ng-multiselect-dropdown": "^0.3.4",
        "ng2-completer": "^9.0.1",
        "ng2-order-pipe": "^0.1.5",
        "ng2-pdf-viewer": "^9.1.5",
        "ngx-owl-carousel-o": "^5.1.0",
        "ngx-slick-carousel": "^0.5.1",
        "ngx-spinner": "11.0.1",
        "ngx-toastr": "14.3.0",
        "ngx-treeview": "^10.0.2",
        "node-sass": "^5.0.0",
        "oidc-client": "^1.11.3",
        "popper.js": "^1.16.0",
        "powerbi-client-angular": "^3.0.5",
        "protractor": "~7.0.0",
        "rxjs": "^6.6.3",
        "slick-carousel": "^1.8.1",
        "sort-by": "^1.2.0",
        "tslib": "^2.3.1",
        "xlsx": "^0.18.3",
        "zone.js": "~0.11.4"
    },
    "devDependencies": {
        "@angular-devkit/build-angular": "~13.2.5",
        "@angular/cli": "^13.2.5",
        "@angular/compiler-cli": "^13.2.4",
        "@angular/language-service": "^13.2.4",
        "@types/jasmine": "~3.6.0",
        "@types/jasminewd2": "~2.0.8",
        "@types/jquery": "^3.5.6",
        "@types/node": "^12.20.24",
        "codelyzer": "^6.0.0",
        "ini": "^1.3.7",
        "jasmine-core": "~3.6.0",
        "jasmine-spec-reporter": "~5.0.0",
        "karma": "~6.3.4",
        "karma-chrome-launcher": "~3.1.0",
        "karma-coverage-istanbul-reporter": "~3.0.2",
        "karma-jasmine": "~4.0.0",
        "karma-jasmine-html-reporter": "^1.5.0",
        "ng-packagr": "^13.2.1",
        "typescript": "~4.5.5"
    },
    "optionalDependencies": {
        "ts-node": "~8.4.1",
        "tslint": "~5.20.0"
    }
}

解决方法

1. 直接指定版本安装

跳过npm update,直接安装目标版本的Lodash:

npm install lodash@4.17.21 --save

该命令会强制安装4.17.21版本,同时将Lodash显式添加到package.json的dependencies中(如果之前未声明),确保项目优先使用指定版本。

2. 强制覆盖子依赖版本(适用于npm 8+)

从依赖树可以看到,ngx-treeview、node-sass等依赖依赖的是Lodash 4.17.20,导致版本不一致。可以使用npm的overrides功能强制所有子依赖使用目标版本:
在package.json中添加如下配置:

"overrides": {
  "lodash": "4.17.21"
}

添加完成后执行:

npm install

npm会自动将所有依赖树中的Lodash替换为4.17.21版本。

3. 清理缓存后重试

如果以上方法均无效,先清理npm缓存并重新安装依赖:

npm cache clean --force
rm -rf node_modules package-lock.json
npm install

完成后再执行步骤1的安装命令。

内容的提问来源于stack exchange,提问作者aquib manzar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:35:55