当前位置:网站首页>Three simple tips for accelerating yarn install
Three simple tips for accelerating yarn install
2022-06-23 15:30:00 【Code Taoist】

Improving the productivity and quality of life of developers is my passion . Accelerating the use of code is not only interesting , It is also essential to build products quickly . Happier and faster developers are equal to happier customers , Because they can get features and bug fixes faster !
Based on npm The ecosystem and its myriad packages , Installation time of the dependency , Especially in large projects , It may be too long . Here are three simple tips , Can help you save oneortwo minutes , Sometimes it can even reduce yarn install Half the time
bloated yarn.lock problem
If your project is in yarn Up operation , Then some of your dependencies are likely to be copied , Even if they satisfy semver Conditions . Even though yarn promise Data deduplication is not required , But it's not true .
Imagine this : You are putting @awesome/tools Add libraries to dependencies , It also depends on [email protected]^1.0.0 library , This is the latest version of it . install @awesome/tools after , You will be in yarn.lock In the file :
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"[email protected]^1.0.0":
version "1.0.0" After a few months , You want to add another library that depends on these utilities , for instance @simple/button.Utils The library also released some bug fixes and features , The latest version of it is 1.5.1, also @simple/button Depend on it . If you just run yarn add @simple/button, Then you will be in yarn.lock see :
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/[email protected]":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"[email protected]^1.0.0":
version "1.0.0"
"[email protected]^1.5.1":
version "1.5.1" Even if 1.5.1 and 1.0.0 The version is semver Compatible ,yarn Nor will they be merged into one as you would expect , You'll end up in repo Get two identical versions of utils.
If you have several different libraries that depend on 1.0.0, The others depend on 1.5.1,yarn One of the versions will be promoted to the root of the folder node_modules Catalog , But the other will have nowhere to go ( Only one version can be located in the root directory ) , And they will be installed as copies in node_modules Use their libraries in the folder . You will eventually get this folder structure :
/project
/node_modules
/ [email protected]
/@simple/button
/node_modules/ [email protected]
/@simple/form
/node_modules/ [email protected]
/@simple/select
/node_modules/ [email protected] Although it seems that you only have 2 Versions utils library , But actually , It can be an infinite number of copies that exist in your project , All of this needs to be copied to their location , All this will steal your yarn install Time for .
Solution
duplicate removal ! Or manually make sure that all semver Compatible libraries are resolved to one version , Or use yarn-deduplicate And so on . This is what you want yarn.lock:
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/[email protected]":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"[email protected]^1.0.0", "[email protected]^1.5.1":
version "1.5.1" For example , In a project , I am completely deduplicating all dependencies , take yarn install The time from 3 Minutes to 1.5 minute .
monorepo and workspaces The dark side of
If you use monorepo and yarn work area ( or Lerna, It uses the following workspace ) To manage your package , You can easily find that your dependencies in the local package are out of sync with each other . Similar to the above , If one of your packages depends on [email protected]^1.0.0, Other packages depend on [email protected]^1.5.1, And these packages are not deduplicated in the root directory ,yarn Multiple copies of versions that are not promoted to the root directory will be created , And install them in your workspace node_modules In the folder . under these circumstances , You may not only end up in the root node_modules Multiple copies of the same dependency in , And there are also scattered in repo Copy of the same dependency in :
/project
/node_modules
/ [email protected]
/ packages
/one
/node_modules/ [email protected]
/two
/node_modules/ [email protected] In a very large project I'm working on , It has 600 Multiple workspaces , If we allow only 10% Dependencies of are out of sync ,yarn install It will take an hour , instead of 5 minute .
Solution
If you move to yarn2 Or anything based on pnpm It's not a good choice ( They solved this particular problem ), Then the only other option is to keep the dependencies and roots in the workspace package.json Strict synchronization .
Nuke node_modules
For some reason , When updated yarn When something strange happens after a dependency in , The first solution everyone recommends is nuke nodemodules Folder , And execute a new yarn install. This is usually the last suggestion , Because it miraculously fixes 90% The strange situation of . After a while , You will form a habit , Each time you check the main branch and start developing new functionality, you execute `rm -rf nodemodules && yarn install`.
If only I could speed up a little more , At least speed up 50%……
Solution
At your root node_modules There is a secret file in the folder .yarn-integrity,yarn Write down all the information it needs to know about your repository and its installed dependencies . If the contents of this file are similar to repo The situation in does not match ,yarn It will be updated and the installed packages will be refreshed . If the file is missing , It will be based on yarn.lock Generate it , And update the node_modules The content in . So if most of the content is already correct , Then it just replaces the incorrect content .
We didn't execute rm -rf **node_modules** && yarn install, But to perform rm -rf **node_modules/.yarn-integrity** && yarn install, It can be effectively reduced by half for yarn install Time for .
original text :https://adevnadia.medium.com/three-simple-tricks-to-speed-up-yarn-install-9cae57d159db
边栏推荐
- 山东:美食“隐藏款”,消费“扫地僧”
- Sorting out and summarizing the handling schemes for the three major exceptions of redis cache
- Force deduction solution summary 513- find the value of the lower left corner of the tree
- JS里的数组
- golang 重要知识:RWMutex 读写锁分析
- Introduction to the push function in JS
- The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service
- Simple understanding of quick sort
- MySQL advanced statement I
- Origin of sectigo (Comodo) Certificate
猜你喜欢
Explain in detail the principle and implementation of redis distributed lock

Millions of bonuses are waiting for you to get. The first China Yuan universe innovation and application competition is in hot Recruitment!

Raspberry PI installing the wiring pi

golang 重要知识:mutex

Six programming insights in these five years!

volatile~多线程下变量不可见

golang 重要知识:定时器 timer

Important knowledge of golang: rwmutex read / write lock analysis

JS垃圾回收

进销存软件排行榜前十名!
随机推荐
F5《2022年应用策略现状报告》:边缘部署及负载安全成亚太地区关注焦点
基因检测,如何帮助患者对抗疾病?
2021-06-07
Starting from 3, add paging function in the business system
After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world
Golang -- multiple processing scenarios for files
2021-05-08
golang 重要知识:sync.Once 讲解
Important knowledge of golang: atomic atomic operation
[datahub] LinkedIn datahub learning notes
这届文娱人,将副业做成了主业
Introduction to the push function in JS
Volatile~ variables are not visible under multithreading
MySQL高级语句二
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
[pyside2] pyside2 window is on the top of Maya (note)
Gartner's latest report: development of low code application development platform in China
进销存软件排行榜前十名!
力扣解法汇总513-找树左下角的值
Une compréhension simple du tri rapide