I recently encountered a yarn bug. After searching, I found that it existed for 6 years. What kind of magical problem is this? After some analysis and investigation, I gave 6 solutions. . .
1. Problem description
The package manager of several projects I have taken over recently is yarn@v1.22.19
. After installing the dependencies, no matter what Whether it is successful or not, there are always network connection problems and it will be stuck for a long time, and then there will be a few lines of abnormal logs like this: info There appears to be trouble with your network connection. Retrying...
.
Sometimes some magical packages (such as node-sass
) have exceptions and cause the installation to fail. As a result, the failure is discovered after being stuck for a long time, which is really frustrating. In addition, there are dozens of related issue
in the github
warehouse of yarn
, with a time span of 6 years from 2016 to 2022. There are different opinions on the reasons and solutions. I was very curious about what kind of magical problem this was that had not been solved for 6 years, so I decided to find out. [Recommended related tutorials: nodejs video tutorial, Programming video]
2. Troubleshooting
2.1. Keyword search
2.1.1. Search github
When you encounter problems and errors that don’t have any ideas, The first tip is to search for it. Search the github
warehouse of yarn
for error informationThere appears to be trouble with your network connection
, you can see that there are 1 related code and 91 in the results Related issue
. I searched for a while in issue
but couldn't find a suitable solution, so I went to the next step: search the code.
2.1.2. Search code
Due to network reasons, go directly to the local yarn
installation directory to search. Use vscode
to open the installation directory of yarn
(my local directory is ~/.volta/tools/image/yarn/1.22.19
), and search globally for the key WordThere appears to be trouble with your network connection
. You can see that there is also only one result, and the entire error message is assigned to a variable offlineRetrying
.
Global search keywordsofflineRetrying
There are 2 results in total, except for the results in the previous step, there is only 1 reference. The code here mainly throws exceptions and retries, and there are no more keywords to dig out. Next, enter the debugging process, put a breakpoint in front of the offlineRetrying
line of code, and debug to see the specific error message and context.
2.2. Program debugging
2.2.1. Determine the debugging command
You need to run the command to install dependenciesyarn
, So how to debug it? yarn
is a npm
package. When executed, it actually calls node xxx.js
, and this xxx.js
is generally configured in ## In the bin
field of #package.json. As you can see from the picture below, the corresponding file for yarn is
./bin/yarn.js, so you can use this line of debugging command:
node --inspect-brk ~/ .volta/tools/image/yarn/1.22.19/bin/yarn.js. (For debugging of
Node.js, please refer to
Official Documentation)
First add the
debugger statement before the code line cli.js:66099
where the variable offlineRetrying
is located.
Then go back to the root directory of the business project and run the debugging command node --inspect-brk ~/.volta/tools/image/yarn/1.22.19/bin/yarn.js
. At this time, the program hangs waiting for the debugging tool to connect, and prints out the following log:
Then open the chrome
built-in debugging pagechrome://inspect/#devices
, find Target
with the same file path, and click the inspect
button to start debugging.
Then chrome
will open an independent DevTools
window, because node --inspect-brk is used
command, at this time DevTools
the automatic breakpoint is at the beginning of the file being debugged, you need to press F8
to skip the breakpoint and continue execution.
After waiting for a short period of time, DevTools
stops at the previously added breakpoint. You can see that this is a timeout exception, and the request that caused the exception is GET: https://yarnpkg.com/latest-version
. Using curl
to request this link results in a 210s timeout. Accessing this link using a proxy can succeed, but the request is redirected to classic.yarnpkg.com/latest-vers…, and the return result is 1.22.19
.
The problem is now basically clear. The main reason is that the request timed out and multiple retries caused the problem at the beginning of the article. You can use a proxy to avoid this problem. It would be boring if the investigation ended here.
2.2.3. In-depth investigation
In order to further understand yarn
why we need to requestyarnpkg.com/latest-vers…, searched the code using this link as a keyword, and found this keyword chain: https://yarnpkg.com/latest-version
-> SELF_UPDATE_VERSION_URL
-> _checkUpdate
-> checkUpdate
, the actual calling relationship is just the opposite, as shown below:
3. Determine the reason
It has been inferred that the calling relationship of the timeout link is: checkUpdate
-> _checkUpdate
-> SELF_UPDATE_VERSION_URL
-> https://yarnpkg.com/latest-version
, combined with the comments and checkUpdate
function Judging from the code, every time the yarn
installation command is executed, yarnpkg.com/latest-vers… will be requested to check whether there is a new version that needs to be updated. However, this link access times out and will be retried after failure. The default timeout is 30s and the number of retries is 4, so after the installation is completed, it will still be stuck for 120s before the program actually ends.
4. Solution
There are three key factors that cause problems: checking for updates, timeout, and retrying. Therefore, you can optimize the network, adjust the timeout, and jump There are 3 directions to solve the problem by checking and updating. There are 6 solutions below for reference.
4.1. Optimize the network
It is easy to think of this idea. Since the access times out, then increase the request speed.
- [Option 1] Use a proxy to optimize the network (recommended)
$ yarn install --proxy "http://{domain}:{port}" --https-proxy "http://{domain}:{port}"
Taking my development environment as an example, the command looks like this:
yarn install --proxy "http://10.180.55.191:7890" --https-proxy "http://10.180.55.191:7890"
4.2. Adjust the timeout period
This idea is relatively straightforward and applicable to more scenarios. If other methods don’t work, you can try it.
- [Option 2] Modify network-timeout (depending on the situation)
The default timeout is 30s and can be changed to 2s. After modification, the exception still exists, but it can be Checking for updates fails quickly without waiting for several minutes.
yarn install --network-timeout 2000
Some developers said that the exception occurred because some large
npm
packages took too long to install and exceeded the default timeout of 30s, so the network-timeout can also be changed. Avoid exceptions.
4.3. Skip check update
The solution to this idea mainly comes from several termination conditions in the checkUpdate
function.
- 【方案3】修改配置禁止检查更新(推荐)
$ yarn config set disable-self-update-check true$ yarn install
- 【方案4】修改配置把上次更新时间调到百年后(推荐)
$ yarn config set lastUpdateCheck 1e13 $ yarn install
- 【方案5】执行命令时禁用交互式提示(推荐)
$ yarn install --non-interactive
- 【方案6】修改代码跳过检查更新(不推荐)
- 找到
yarn
的安装目录注释checkUpdate
的调用,具体代码行为cli.js:7261
,修改后长这样:// this.checkUpdate();
- 也可以修改其他可以阻断
checkUpdate
函数的代码...
- 找到
5、最后
以上主要是分享一些问题分析排查的经验,另外也提供了一些 yarn install
超时异常的解决方案,希望能对前端同学们有所帮助。
在快写完这篇文章的时候,yarnpkg.com/latest-vers… 已经可以正常访问,不知道还会不会有人再遇到这个问题。
另外我在
yarn
的github issue
中回复了以上的解决方案,希望前端同学们少受点折磨,也希望官方早点修复这个6年陈的老Bug。?
更多编程相关知识,请访问:编程教学!!
The above is the detailed content of Record and analyze a yarn bug that has existed for 6 years. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
