“链接依赖"是什么意思?在 npm/yarn install 期间真的吗? [英] What does "Linking Dependencies" during npm / yarn install really do?

查看:57
本文介绍了“链接依赖"是什么意思?在 npm/yarn install 期间真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大型网络应用程序 npm install 和.yarn install 确实需要很多时间,主要是在一个称为 Linking Dependencies 的步骤中.这里发生了什么?它是否正在获取依赖项的依赖项?或者完全不同的东西?在此步骤中创建了哪些文件?

解决方案

当你调用 yarn install 时,以下事情会依次发生:

  1. 解决:Yarn 通过向注册表发出请求并递归查找每个依赖项来开始解决依赖项.

  2. 下载/获取:接下来,Yarn 在全局缓存目录中查找所需的包是否已经下载.如果没有,Yarn 会获取包的 tarball 并将其放在全局缓存中,这样它就可以脱机工作并且不需要多次下载依赖项.也可以将依赖项作为 tarball 放置在源代码管理中,以便完全离线安装.

  3. 链接:最后,Yarn 将所有需要的文件从全局缓存中复制到本地 node_modules 目录中,然后确定哪些内容已经存在,哪些不存在,从而将所有内容链接在一起.

<小时><块引用>

yarn install 确实需要很多时间,主要是在一个叫做 Linking Dependencies

的步骤中

您应该注意到 Step 3: LinkingStep 1: ResolutionStep 2: Fetching 花费的时间更多发生.到这一步我们已经准备好了需要下载的东西,那为什么要花很长时间,我们有没有遗漏什么?

是的,复制到本地项目到node_modules文件夹...!这样做的原因是这个副本不等同于复制一个 4.7GB 的大 ISO 文件.相反,它是多个超小文件(当我说多个时不要轻信,它可以是 15k+ 个文件:P),因此需要大量时间来复制.(另外,重要的是要注意,当你下载包时,你会为每个包下载一个大的 tar 文件,然后应该将其内容提取到缓存中,这也需要时间)

速度较慢

  • 防病毒:您的防病毒软件处于中间位置,并在每个文件上进行快速检查(除了我们的纱线检查是否已存在),纱线试图复制并降低其速度这么多.如果您使用的是 Windows,请尝试将您项目的父文件夹作为例外添加到 Windows Defender.
  • 存储介质的传输速率:SSD 可以极大地提高此速度(抱歉,SSHD 和 FireCudas 也无济于事,这将是一次).
<小时>

但是这样有效吗?我可以从全局 node_modules 中获取它吗(创建一个之后)?

两个问题都不行.由于节点的工作方式,每个包只能相对于自己的位置找到它的依赖项.也因为每个项目可能希望使用同一包的不同版本,以确保其正常工作且不会因包更新而中断.

理想情况下,项目文件夹应该精简.一种有效的方法是拥有一个 global node_modules 文件夹.下载任何和所有请求的包(如果尚未存在)并从此位置使用.实际上Ruby 就是这样做的.这是我的全局 Ruby 等效于 node_modules 文件夹.请注意在不同项目中使用的同一包的不同版本.

但请记住,这会降低项目的可移植性.这是任何管理器(无论是 ruby​​gems 还是节点模块)都必须做出的权衡.我可以只复制节点项目文件夹(实际上可能需要几个小时,因为您还将复制(本地)node_modules 文件夹,但如果我只有该项目文件夹,我可以期望它可以工作,而不是复制一个 ruby​​ 项目只需几秒钟到几分钟,因为没有本地包(或他们称之为 gems)文件夹,但是在不同的系统上运行项目将需要这些包存在于全局 gems 上文件夹.

For large web apps npm install resp. yarn install does take a lot of time, mostly in a step called Linking Dependencies. What is happening here? Is it fetching the dependencies of the dependencies? Or something completely different? Which files are created during this step?

解决方案

When you call yarn install, the following things happen in order:

  1. Resolution: Yarn starts resolving dependencies by making requests to the registry and recursively looking up each dependency.

  2. Downloading/Fetching: Next, Yarn looks in a global cache directory to see if the package needed has already been downloaded. If it hasn't, Yarn fetches the tarball for the package and places it in the global cache so it can work offline and won't need to download dependencies more than once. Dependencies can also be placed in source control as tarballs for full offline installs.

  3. Linking: Finally, Yarn links everything together by copying all the files needed from the global cache into the local node_modules directory after identifying what's already there and what's not there.


yarn install does take a lot of time, mostly in a step called Linking Dependencies

You should notice that Step 3: Linking is taking more time than Step 1: Resolution and Step 2: Fetching where the actual download happens. During by this step we already have things that we need ready and downloaded, then why is it taking long, did we miss anything?

Yes, COPY to local project into node_modules folder...! The reason for this is that this copy is not equivalent to copying one large 4.7GB ISO file. Instead it's multiple super small files (Don't take it light when I say multiple, it can be 15k+ files :P ), hence take a lot of time to copy. (Also, it is important to note that when you download the packages, you download one large tar file per package, whose contents should then be extracted into the cache which also takes time)

It is slower due to

  • Anti-virus: Your antivirus is sitting in the middle and doing a quick inspect (in addition to our yarn checking if it already exists) on every single file yarn is trying to copy cutting its speed by so much. If you are on Windows, try adding your project's parent folder as exception to Windows Defender.
  • Storage medium's transfer rate: SSDs can improve this speed hugely (Sorry, SSHDs and FireCudas will not help either, this is gonna be one time).

But is this efficient? Can I have it taken from the global node_modules (after creating one)?

Nope for both questions. Because of the way node works each package finds its dependencies only relative to its own location. Also because each project may want to use different versions of the same package to ensure its working properly and not broken by package updates.

Ideally, the project folder should be lean. An efficient way of doing this would be to have a global node_modules folder. Any and all requested packages are downloaded if not already present AND used from this location. Actually Ruby does it this way. Here's my global Ruby's equivalent of node_modules folder. Notice the presence of different versions of the same package for use in different projects.

But keep in mind that it would reduce project portability. It's a trade-off that any manager (be it rubygems or node modules) has to make. I can just copy the node project folder (which in fact may take hours because you will be copying the (local) node_modules folder as well, but I can expect it to work if I have just that project folder, as opposed to copying a ruby project would only some seconds to few minutes, as there is no local packages (or gems as they call them) folder, but running the project on different system would require those packages to be present on the global gems folder.

这篇关于“链接依赖"是什么意思?在 npm/yarn install 期间真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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