如何使用`yarn`覆盖嵌套的依赖项? [英] How do I override nested dependencies with `yarn`?

查看:266
本文介绍了如何使用`yarn`覆盖嵌套的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的软件包具有这些依赖项

If my package has these dependencies

{ "name": "my-package",
  "dependencies": { "foobar":"~1.0.3", "baz":"2.0.9" }

foobar软件包具有这些依赖项

And the foobar package has these dependencies

{ "name": "foobar",
  "dependencies": { "baz":"^2.0.0" }

,而baz的最新发行版是2.1.0,则第一次运行yarn会将baz@2.1.0安装在foobar/node_modules中.

and the most recently released version of baz is 2.1.0, the first run of yarn will install baz@2.1.0 in foobar/node_modules.

如何强制纱线将baz@2.0.9包用于foobar?

How do I force yarn to use the baz@2.0.9 package for foobar?

我的理解是,可以使用npm shrinkwrap(la

My understanding is that this would be possible using npm shrinkwrap (a la this question).

我的问题的摘要可能是:纱线创建了可重复的确定性安装,但是如何自定义该安装?

The summary of my question probably is: Yarn creates repeatable, deterministic installations, but how do I customize that installation?

推荐答案

如果您确实具有对所接受版本的限制过于严格的子依赖关系,则可以 使用以下命令覆盖它们纱.

If you do in fact have a sub-dependency that is overly restrictive in what versions it will accept, you can override them using yarn.

更新的编辑:从1.0开始,package.json中添加一个这样的块:

UPDATED Yarn now, as of 1.0, officially supports the "resolutions" block. So the way to override resolutions is to just add a block like this to package.json:

"resolutions": {
      "package-a": "2.0.0",
      "package-b": "5.0.0",
      "package-c": "1.5.2"
}

您有时会收到不兼容"版本的警告,但我发现某些软件包(例如socket.io)在接受的版本上有太多限制,因此,我会很乐意选择实际不会破坏事物的最新版本.

You'll get warnings for "incompatible" versions sometimes, but I find that some packages (like socket.io) are overly restrictive in what version they accept, and so I'll happily select the latest version when it doesn't actually break things.

听起来好像原始问题并不完全正确,但是原始问题实际上是我想要回答的问题,我找到了答案,所以这里是供后代使用.

It sounds like the original question wasn't exactly correct, but the original question was in fact the one I wanted answered, and I found an answer, so here it is for posterity:

我正在使用socket.io库,它具有component-emitter作为依赖项.但是它有一对所需的版本.这是更改任何内容之前的yarn.lock文件的外观:

I'm using the socket.io library, and it has component-emitter as a dependency. But it has a pair of versions that it requires. This is what the yarn.lock file looked like before I changed anything:

component-emitter@1.1.2:
  version "1.1.2"
  resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.1.2.tgz#296594f2753daa63996d2af08d15a95116c9aec3"

component-emitter@1.2.0:
  version "1.2.0"
  resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.0.tgz#ccd113a86388d06482d03de3fc7df98526ba8efe"

因此,在我的客户代码中包含了两个组件发射器的副本.我看了一下,在1.1.2和1.2.0(或最新的1.2.1)之间似乎没有任何重大变化.我首先尝试仅更改yarn.lock文件:

So it was including two copies of the component emitter in my client code. I looked, and there didn't appear to be any breaking changes between 1.1.2 and 1.2.0 (or 1.2.1, which was current). I first tried just changing the yarn.lock file:

component-emitter@1.2.1, component-emitter@^1.2.1, component-emitter@1.1.2:
  version "1.2.1"
  resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6"

这有效,但是文件中有关于它会自动生成的警告,这意味着我添加的每个更新或新程序包都会在此更改上脚.进行了一些搜索,找到了yarn --flat选项,该选项将迫使纱线在整个项目中选择不超过每个卷装的一个.在我看来,这似乎有些过头了,因为我敢肯定,实际情况下,旧版本和新版本的软件包之间是不兼容的.我只是想从我的客户端代码中删除一个多余的软件包,以减小下载量.我仍然希望开发包能够正确地完成所有工作.

This worked, but the file has warnings about it being autogenerated, meaning that every single update or new package I add will stomp on this change. A bit of searching found the yarn --flat option, which will force yarn to choose no more than one of each package in the entire project. That seems like overkill to me, since I'm sure there are actual cases of incompatibility between older and newer packages. I just wanted to eliminate a redundant package from my client code, to make the download smaller; I still want the development packages to all work correctly.

但是在要

But in the docs to yarn --flat I found a reference to a "resolutions" block that can go in package.json:

"resolutions": {
  "package-a": "2.0.0",
  "package-b": "5.0.0",
  "package-c": "1.5.2"
}

因此,我尝试将"component-emitter" : "1.2.1"放在package.json中新的"resolutions"块中,实际上对于需要它的所有位置,它都会将component-emitter展平为1.2.1,现在我只有一个副本了.我的客户代码.

So I tried putting "component-emitter" : "1.2.1" in a new "resolutions" block in my package.json, and it in fact flattened component-emitter to 1.2.1 for all places that required it, and now I have only one copy in my client code.

(现在yarn完全支持resolutions块,因此您甚至不需要使用--flat.)

(And now the resolutions block is completely supported in yarn, so you don't even need to use --flat.)

这篇关于如何使用`yarn`覆盖嵌套的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆