JS new package management tools yarn and NPM contrast and use getting started _javascript tips

Source: Internet
Author: User
Tags generator

This article will introduce yarn in the following ways:

    1. Yarn Compare NPM to solve the problem and what kind of convenience it brings.
    2. Get the correct posture of yarn
    3. Getting Started with yarn (introduction to some common commands
    4. Experience of personal use

Yarn Compare the advantages of NPM

According to the official document yarn has 6 great advantages

1, Off-line mode

Yarn will have a cache directory, will cache the previously installed software packages, the installation will not have to download from the network, greatly accelerate the installation speed.

It's important that NPM has been criticized for having to download a whole bunch of things from the network each time it is installed, and to download it all again, which is more annoying when it comes to engineering.

We deploy node project, it is necessary to install all the dependencies on the publisher and the network environment of the publisher is not very good (not to take the ladder), resulting in the installation of slow and often failed to say (some packages need to be networked compilation). Replace the yarn just yarn cache directory, each install whining fast, hemp no longer worry about publishing failed.

2. Dependence Certainty

The dependency sequence and version of the build are consistent on each machine for the same project installation dependency.

NPM has a bad place to deal with here before. For example, I wrote the project relies on a, B, c three libraries, I was writing Package.json, give A, B, c are specified version number. But A library may also rely on D, E, F libraries, d libraries and rely on G, H library. In so many association dependencies, it is likely that a library does not specify a version number when it specifies dependencies.

So, this leads to a problem. If I rely on the same engineering installation on another machine, or reinstall the Node_modules directory under this machine engineering. Because there is no library with the specified version number in the association dependency, a version update occurs, which results in a reinstallation of dependencies, in which the versions of certain packages are inconsistent. In this case, you will find that the original program can run normally, suddenly become unable to work or a pile of bugs.

NPM is also very sensitive to the order in which packages are introduced, such as executing the following command in an empty project

NPM init-y
npm install globule@0.1.0-s
npm install babel-generator@6.19.0-s
npm Install Babel-helper-define-map@6.18.0-s

We have 3 packages installed here that depend on Lodash, but Globule relies on lodash@1.0.3 and the other two rely on lodash@4.x.

The directory dependencies are now structured as follows

Suppose we use Lodash in the project, but forget to reinstall Lodash

var Lodash = require (' Lodash ');
Console.log (Lodash. VERSION); v1.0.3

Another colleague gets the project code, executes npm install , when the directory-dependent structure is

You can see that the first layer of dependent Lodash becomes the 4.x version, which results in a dependent version inconsistency problem. Yarn, however, guarantees that the directory dependency structure is consistent regardless of the order in which they are introduced, ensuring that no such bugs occur.

3, Network performance optimization

Download the software to optimize the order of requests to avoid the request waterfall to occur

4, the network rebound

Yarn does not cause an installation failure when an installation package request fails, it automatically tries to reinstall. NPM will not hesitate to fail, resulting in another time, time-consuming

5. Multiple registered sources

All dependent packages, regardless of how many times they are referenced indirectly by different libraries, install the package only from a registered source, either NPM or bower, to prevent confusion and inconsistency.

6. Flat mode

For multiple packages that rely on the same child package, yarn will try to extract the same package, preventing multiple replicas from appearing and wasting space. For example, in 1.2, yarn will create the same Lodash child dependency for Babel-generator and Babel-helper-define-map, thus saving a portion of the space.

More emojis.

Expression bag vs. O (╯-╰) o

The correct mounting posture

Note yarn depends on the node to run the environment, the official website provides the different environment of the N installation methods, point I view. One of the most important is also the most general npm install yarn -g , of course, do not know the official website to do so many moths to install the way why is also the brew is Yum, but also toss half a day.

Introduction to yarn common commands

Create a project

Command yarn init , Detailed introduction

Like NPM, an interactive window will appear, asking some package related questions.

Question Name (TestDir): My-awesome-package
question Version (1.0.0): 
question description:the Best package you Would ever find.
Question entry point (index.js): 
question git repository:https://github.com/yarnpkg/example-yarn-package
Question Author:yarn contributor
question license (MIT): Success Saved Package.json ✨done in
87.70s.

Of course, you can add parameters to --yes/-y answer all the questions (yes), easy to generate a Package.json

Management dependencies

Note that the following commands will automatically update your Package.json and Yarn.lock files.

Add dependencies

Command yarn add [package]@[version/tag] , Detailed introduction

This automatically adds the package to the dependencies in the Package.json and also updates the Yarn.lock

 {
 ' name ': ' My-package ',
 ' dependencies ': {
+ ' package-1 ': ' ^1.0.0 '
 }
 }

Adding to a different dependencies requires the following parameters

    1. yarn add --dev/-DAdd to Devdependencies
    2. yarn add --peer/-PAdd to Peerdependencies
    3. yarn add --optional/-OAdd to Optionaldependencies

Update dependencies

Command yarn upgrade [package]@[version/tag] , Detailed introduction

Update a dependent version and automatically update the Package.json and Yarn.lock files

 {
 ' name ': ' My-package ',
 ' dependencies ': {
-' package-1 ': ' ^1.0.0 '
+ ' package-1 ': ' ^2.0.0 '
 }
 }

Delete dependencies

Commandyarn remove [package]

Delete a dependency and automatically update the Package.json and Yarn.lock files

Installation dependencies

Command yarn install , Detailed introduction

All dependencies are extracted from the Package.json and installed, and then generated yarn.lock locks all dependent versions, which are performed yarn install according to Yarn.lock installation dependencies, ensuring that the dependent directory structure of the different computer installations is identical.

Optional parameters

yarn install--flat has and only one dependent version is allowed, multiple dependencies will appear an interactive window, allowing the user to select a version of the installation

yarn install --force force all dependent packets to be downloaded again

yarn install--production Download Dependencies dependencies only

Global command

Adding a global modifier to the yarn command makes the command global, and the supported commands have Add,bin,ls,remove,upgrade

For example npm install gulp -g , you can use yarn global add gulp to substitute

Experience of personal use

Replace the installation source with the acceleration provided by Ali, npm register yarn config set registry 'https://registry.npm.taobao.org' and of course, if NPM is already configured, yarn no longer needs to be configured.

Yarn has a number of minor problems, but the authorities are also trying to fix it, and suggest using it occasionally yarn self-update to update the version

The previous package lock is npm shrinkwrap to use the command, feel cumbersome and difficult to maintain, the use of yarn after the automatic generation of lock files, simple and convenient

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.