【问题标题】:How to force npm to flatten dependencies?如何强制 npm 扁平化依赖关系?
【发布时间】:2018-03-14 10:30:06
【问题描述】:

我有一个项目,其中几个前端共享一个公共库。

这些项目的模块依赖由 npm 管理。

所以,在我拥有的每个项目的 package.json 中:

  "dependencies": {
    "mylib": "file:../<...path...>/mylib",
    ...other deps...
  },

我使用“mylib”有两个目的:

  • 分享一些课程;
  • 共享通用依赖项(主要是角度)

到目前为止,我使用的是 npm 3.3.12,在运行 npm install 之后,mylib 的角度依赖项位于我的顶级项目的 node_modules 目录下。

所以,我有例如

node_modules
    @angular
        core
        common
        ....
    mylib

现在,使用 npm 5.4.2,我有:

node_modules
    mylib
        node_modules
            @angular
                core
                common

这在我的构建过程中导致了很多问题。它需要额外配置打字稿,通过添加指令,例如:

"baseUrl": "",
"paths": {
    "@angular/common": ["node_modules/mylib/node_modules/@angular/common/"],
    "@angular/core": ["node_modules/mylib/node_modules/@angular/core/"],
    "@angular/compiler": ["node_modules/mylib/node_modules/@angular/compiler/"],
    "@angular/compiler-cli": ["node_modules/mylib/node_modules/@angular/compiler-cli/"],
    "@angular/forms": ["node_modules/mylib/node_modules/@angular/forms/"],
    "@angular/http": ["node_modules/mylib/node_modules/@angular/http/"],
    "@angular/platform-browser": ["node_modules/mylib/node_modules/@angular/platform-browser/"],
    "@angular/platform-browser/animations": ["node_modules/mylib/node_modules/@angular/platform-browser/animations/"],
    "@angular/platform-browser-dynamic": ["node_modules/mylib/node_modules/@angular/platform-browser-dynamic/"],
    "@angular/router": ["node_modules/mylib/node_modules/@angular/router/"],
    "primeng/primeng": ["node_modules/mylib/node_modules/primeng/primeng"],
    "rxjs/Rx": ["node_modules/mylib/node_modules/rxjs/Rx"]
    }

在 tsconfig.json 中

当你不得不为 AOT、rollup 等做类似的配置时,这真的很烦人......

我尝试使用 npm dedupe 来简化这一点。由于项目有很多依赖项,仅其中一个项目就需要超过 1000 万个:

npm dedupe
...
...
removed 824 packages and moved 1020 packages in 623.196s

是否有一种标准的、高效的方式来使依赖项与以前一样扁平化? npm dedupe 可以完成这项工作,但需要花费太多时间,因此它不是一个可接受的替代方案。

【问题讨论】:

  • 您是否尝试将所有这些依赖项添加为 myLib 中的 peerDependencies?
  • 我不明白它对我有什么帮助。我想在 myLib 中指定 dep。使用 peerDependencies,我必须在我的顶级项目中添加这些部门,不是吗?而且我的东西​​依赖于“myLib”中的那些依赖项......
  • 看看nodejs.org/en/blog/npm/peer-dependencies 我认为在您的情况下,您希望 myLib 作为 peerDependency 和其他项目中的这些依赖项
  • 尝试改用yarn。安装它然后删除您的node_modules 文件夹并执行yarn install --flat。或者只是 yarn install 仍然应该进行重复数据删除。
  • npm dedupe 对我在这里找到非常有帮助,谢谢

标签: angular typescript npm angular-aot


【解决方案1】:

作为npm 的替代方案,您可能希望改用yarn。默认情况下,这应该对模块进行重复数据删除。首先删除您现有的node_modules 文件夹,然后执行yarn install

您也可以强制 yarn 进行平面安装 (yarn install --flat),但在这种情况下,只需进行普通安装就足够了。

yarn.lock 文件添加到版本控制,然后任何其他签出都将被锁定到相同的模块版本(除非他们这样做yarn upgrade)。

【讨论】:

  • 处理本地依赖,开发团队成员使用各种操作系统,yarn 是解决这个问题的唯一可靠方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 2019-04-23
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
相关资源
最近更新 更多