可以在Angular 4中摆脱lodash吗?

时间:2018-12-21 13:24:11

标签: angular lodash

最近,我发现发布后lodash在我的Angular 4应用程序中使用了超过500 KB。我删除了所有lodash用法并删除了其软件包。

但是现在我在构建时遇到了问题:

  

错误:找不到模块“ lodash”

Angular是否需要lodash?无法将其删除吗?

[编辑]

我删除了所有依赖项,但仍然无法启动或构建应用程序。 但是当我使用时:

npm install

结果是:

`-- @angular/cli@1.2.7
    `-- lodash@4.17.11

然后该应用程序开始运行,但是它再次具有lodash,尽管package.json中未提及。看来angular-cli要求...

package.json:

{
  "name": "agent-reporting-client",
  "version": "1.4.1",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "npm run meta && ng serve -o --port 185 --host localdev.moneygram.com",
    "build": "ng build",
    "test": "ng test -sm='false'",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "publish": "npm run meta && ng build --aot --prod --environment=prod",
    "debug-aot": "ng build --aot --environment=prod --output-hashing none",
    "meta": "node meta/build src src/metaDefinitions.ts"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "4.3.0",
    "@angular/common": "4.3.0",
    "@angular/compiler": "4.3.0",
    "@angular/core": "4.3.0",
    "@angular/forms": "4.3.0",
    "@angular/http": "4.3.0",
    "@angular/platform-browser": "4.3.0",
    "@angular/platform-browser-dynamic": "4.3.0",
    "@angular/router": "4.3.0",
    "@ngx-translate/core": "7.0.0",
    "@ngx-translate/http-loader": "0.1.0",
    "babel-polyfill": "^6.26.0",
    "bootstrap": "3.3.7",
    "core-js": "2.4.1",
    "es5-shim": "4.5.9",
    "es6-shim": "0.35.3",
    "file-saver": "1.3.3",
    "jquery": "3.2.1",
    "moment": "2.18.1",
    "ng2-webstorage": "1.8.0",
    "ngx-bootstrap": "2.0.5",
    "nodegit": "0.20.2",
    "oidc-client": "1.5.4",
    "rxjs": "5.4.3",
    "zone.js": "0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.2.7",
    "@angular/compiler-cli": "4.3.0",
    "@angular/language-service": "4.3.0",
    "@types/jasmine": "2.5.53",
    "@types/jasminewd2": "2.0.2",
    "@types/node": "6.0.60",
    "codelyzer": "3.0.1",
    "jasmine-core": "2.6.2",
    "jasmine-spec-reporter": "4.1.0",
    "karma": "1.7.0",
    "karma-chrome-launcher": "2.1.1",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "1.2.1",
    "karma-jasmine": "1.1.2",
    "karma-jasmine-html-reporter": "0.2.2",
    "node-sass": "4.5.3",
    "protractor": "5.1.2",
    "ts-node": "3.0.4",
    "tslint": "5.3.2",
    "typescript": "2.3.3",
    "uglifyjs-webpack-plugin": "0.4.6",
    "webpack-bundle-analyzer": "^3.0.3"
  }
}

npm ls lodash:

+-- @angular/cli@1.2.7
| `-- lodash@4.17.11
+-- karma@1.7.0
| `-- lodash@3.10.1
`-- webpack-bundle-analyzer@3.0.3
  `-- lodash@4.17.11

感谢您的帮助...

1 个答案:

答案 0 :(得分:2)

仅仅因为它在CLI中并不意味着它就在您的编译输出中。 CLI是一种工具。这就是为什么它依赖于devDependencies而不是package.json中的依赖关系。它不会被编译到您的Angular代码的输出中。

但是,如果您是在dev中本地提供应用程序,那么Angular CLI可能会加载lodash作为CLI依赖项的一部分。

如果您的应用程序未直接使用lodash(在项目中搜索“ from'lodash”),则不会将其捆绑到最终输出中。