[Web front end] uses yarn instead of NPM as a module manager for node. js

Source: Internet
Author: User

CP from:https://www.jianshu.com/p/bfe96f89da0e Fast, reliable, and secure dependency management

Yarn is a software produced by Facebook to manage the Nodejs package, and the students who have developed nodejs should know that we generally use NPM as the module manager for our Nodejs project, but NPM has some legacy issues, first of all, NPM installs slowly, And when the number of modules in the project is getting more and more, it is a headache to manage these modules and their dependent versions, and then NPM can execute the program in the module installation process, the security is not high. Yarn exists just to solve these problems, the module installation speed is almost outrageous, when some modules through yarn installation, re-installed and even do not need to connect to the network, yarn can also be very good control module dependent version (locked by Yarn.lock file), and safe and reliable. Personally think that this ox x thing will certainly replace NPM in the future, and will do better and more perfect.

0X01 Installation

Windows version: Very simple, download the installation files can be run. Note, be sure to turn off the anti-virus software, or add the installation program to the white list of anti-virus software, otherwise it may not be successful installation, the author has been in this pit for a long time.

Linux version Installation instructions: It is also very simple, if you are using Debian or Ubuntu, use the command directly apt-get install yarn , other Linux version just follow the URL above the document installation.

0x02 using yarn

Here are some common commands:

    • yarnAnd yarn install , the effect of these two commands is the same as npm install , using this command will generate a Yarn.lock file in that directory.
    • yarn add koa, install the koa module and update the Package.json and Yarn.lock files, equivalent to npm install koa --save . can also be used yarn global add koa , equivalent to the npm install koa -g module directly installed into the global environment variable, easy to use.
    • yarn listTo view the dependencies and versions of the module based on the Package.json of the current project.
    • yarn info koaTo view the details of the koa module, similar to the npm view koa .
    • yarn init, initialize the project Package.json file, equivalent to npm init .
    • yarn run, run Package.json in the script .

Yarn also has a lot of useful commands, very simple, you can yarn help get help information, or see yarn on the website of the document, the above is very detailed.

0x03 yarn.lock File

Before saying that NPM has some historical legacy issues, see:

Dependency of NPM Modules
For example, your project module dependencies are depicted in the diagram, @1.2.1Represents the version of this module. When you install a, you need to install dependent C and D, a lot of dependencies will not specify the version number, the default is to install the latest version, which will cause problems: such as the installation of the module today, C and D is a version, and when the C, D updates, the installation module will install the latest version of C and D, If the new version is not compatible with your project, your program may be out of a bug or even run. This is the disadvantage of NPM, and yarn in order to solve this problem introduced a yarn.lock mechanism, this is the author of the project Yarn.lock file. Yarn.lock file format

As you can see, this file has locked the version number of the dependent module, and when you do yarn install , yarn will read the file to get the dependent version number, and then follow this version number to install the corresponding dependent module, so that the dependency will be locked, no longer worry about the issue of the version number. Other people or other circumstances in use, the Yarn.lock copy to the corresponding environment project and then install it, note: This file do not manually modify it, when you use some operations such as yarn add , yarn will automatically update yarn.lock.

Yarn official website



Vegetable Litchi
Links: https://www.jianshu.com/p/bfe96f89da0e
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

[Web front end] uses yarn instead of NPM as a module manager for node. js

Related Article

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.