Introduction and installation of Node. js Package Manager Yarn

Source: Internet
Author: User
Tags arch linux
We all know that before yarn was released, all Nodejs developers used npm package management tools, and npm tools had a lot of intolerable criticism, this includes slow installation speed and online re-installation every time. yarn is designed to solve the current npm problems. This article introduces the Package Manager Yarn and the installation method. Let's take a look. Preface

During the past two days, has Yarn been quietly screen-hunting? Recently, Facebook released a new node. js Package Manager Yarn to replace npm. In order to keep up with the tide of Javascript, the self-proclaimed package management is fast, reliable, and secure, so the content written will not be very detailed, it is more likely to compare the differences between this brand-new package management and npm. It may be wrong in some places. If so, please correct me.

I. Installation

First of all, install it. Unlike the Package Manager installed with nodejs, which is named npm, Yarn needs to be manually installed. This is understandable. After all, it is a brand new thing. But to be honest, the installation experience is not good ...... Right.

Why ...... Because my development environment is Arch Linux, the official documentation only provides installation methods for apt-get Linux and yum Linux ......

Since npm is expected to be replaced, there must be a full-platform compatibility solution. As a result, there must be a manual installation script for all release versions. So I tried this, but it was not installed once three times, various problems ......

Finally, we found that yarn can be directly installed with npm like the module:

npm install -g yarn

What should I use npm to replace npm? Okay, no matter how you say it is installed like this, so I say the installation experience is normal ...... It may be better for other releases. For some popular releases and macOS, installing a command is a matter of entering the terminal.

Although the installation method on Arch Linux has been available by the time I write this article, it is only through the yaourt (AUR) method, however, I don't think it is easier for a newbie to manually install yaourt and then install yarn than to directly install nodejs and then install npm. Maybe at this stage, Arch is a niche release than Debian/Ubuntu/CentOS ......

Ii. Dependencies Management

The installation section has come to an end. As a package management, we must do our duty well. Next, I want to try whether the new package management is as amazing as it is. Next, I will compare yarn, npm, and cnpm in the same environment. The npm registry has been replaced with a https://registry.npm.taobao.org due to a special environment inside the wall.

FASTER

Before the comparison, I think this comparison is unfair to yarn because npm has already set up domestic images, and yarn does not have domestic images as of the end of this article, therefore, no special settings are made for yarn. I am somewhat worried about whether this will cause errors to the test results.

Since Yarn is a Facebook family, let's first try installing its React:

Next is npm. Because npm does not display the command time, I have a stopwatch. It looks like 45 s, more than several times that of yarn. It seems that yarn is faster than npm, and npm uses domestic sources.

However, the cnpm speed is faster, with only the 4S to complete the entire installation process. However, after all, cnpm is a derivative product of npm, which is reasonable. Since this can improve the speed of npm, who knows if cyarn will be available in the future? However, through this test, we can see that the speed of yarn is a qualitative leap compared with npm, and FASTER is not a waste of name.

Next, I tried to install other packages and compared them multiple times. On average, yarn is 3-6 times faster than npm (this is the case of a smaller package ).

RELIABLE/SECURE

As to whether the package management is reliable/secure, I have no idea how to test it for the moment. I only know that when I use it, sometimes there is a chance that some inexplicable errors will be reported, but if I execute the command twice, it will work again normally. After all, yarn is new. However, within this day, yarn's GitHub project home page has won 9 K star, and there are also thousands of issues and pull requests. It can be seen that it is very popular, so I am very optimistic about its prospects.

Offline mode

One of the highlights of Yarn is that dependencies can be installed offline. Of course, the premise is that this dependency has been installed. Yarn caches installed packages. If the-offline parameter is specified during the next installation, Yarn Directly extracts the package from the cache, which greatly shortens the dependency installation time.

Unfortunately, although I have read the official documents once and again, this function still cannot work normally in my environment. I will find the reason. I will add this paragraph later.

However, this feature is truly eye-catching. It can be said that it solves a short board of npm and is a fatal short board. With this feature, you can install existing dependencies in a non-network environment. In addition, the download time is reduced, greatly improving the efficiency. Imagine that it will not take 15 minutes to execute react-native init later (of course, when does Facebook change the react-native init installation dependency process to its own package management installation ).

Yarn. lock File

Yarn uses a yarn. lock file to allow your program to have a consistent experience on different machines. Although this explanation is far-fetched, I have no chance to show it to you. It is very conscientious ). It seems to be a very high-end thing. In fact, it is mainly a supplement to package. json.

If you are interested, you can refer to the detailed introduction here (the English is better ):

Yarn

Fast, reliable, and secure dependency management. yarnpkg.com

Summary

Sometimes it's just getting your eyes closed, and a new thing is coming out of the dark. Today, yarn is released. What will happen tomorrow. Fortunately, we can catch up with the early bus of this new thing.

However, it must be said that the emergence of yarn is indeed progressive. It solves some known problems of npm and expands new features. In this case, it is worth accessing and learning. Although it is still a long time to replace npm and take a long journey, it is foreseeable that its future will be bright.

The above is all the content of this article. I hope this article will help you in your study or work.

For more information about getting started with Node. js Package Manager Yarn and installing Yarn, see PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.