Yarn, like NPM, is a package management tool, so what is the difference between the two, and which one should we use

So let’s take a closer look. What are they

One of NPM.

NPM is one of the main reasons node.js is so successful

NPM is designed around the idea of semantic versioning. Here is an excerpt from their website:

Given a version number: the major version number. Minor version. Patch version

Major version number: When an API changes and is incompatible with a previous version

Secondary version number: when functionality is added but backward compatible

Patch version number: when backwards-compatible defects are fixed

NPM uses a file called package.json in which the user can save all the dependencies in the project by using the NPM install –save command

Here is the version number of a dependency in package.json:

"5.0.3", "~ 5.0.3", "^ 5.0.3." "

5.0.3 indicates the installation of the specified 5.0.3 version

~ 5.0.3 indicates that the latest version of 5.0.x is installed

^5.0.3: Install the latest version of 5.x. x

This is quite disgusting, so there will often be the same project, some colleagues can run, but some colleagues will be because the version of the installation is not consistent with the very weird bug, we often hear a sentence: “on my computer can………”

Also, many NPM libraries are heavily dependent on other NPM libraries, which leads to nested dependencies and increases the probability of failing to match the corresponding version, resulting in errors

During installation, packages are downloaded and installed at the same time, and at some point along the way, a package fails, but NPM continues to download and install packages. Because NPM prints all the logs to the terminal, the error information about the error package gets lost in the pile of warnings that NPM prints, so it’s hard to find the actual package that went wrong and why

In addition, if we install NPM dependencies before, but then execute NPM install, then the dependencies will be re-downloaded from the network, which will become very slow.

To sum up, to summarize the disadvantages of NPM:

1. Version inconsistency may occur due to the impact of version numbers

2. Because the NPM inventory is nested with dependencies, it will increase the probability of failing to match the corresponding version

3. The output information of NPM is verbose. When NPM install is executed, the command line continuously prints out all installed dependencies, which is not concise enough

4. You need to download it from the network rather than directly from the cache

5. Dependencies are downloaded one by one. You can download the next one only after the previous one has been downloaded

 

2. CNPM

CNPM is used exactly the same as NPM, except that NPM is changed to CNPM when the command is executed.

The NPM installation plug-in is downloaded from a foreign server and may be abnormal due to network impact.

Taobao first requests the content of foreign servers to its own domestic server, so that when we use CNPM, we download the content from the domestic server, and the speed is much faster.

The website says: “This is a full nPMjs.org image, which you can use instead of the official version. The sync frequency is currently 10 minutes to ensure maximum synchronization with the official service.”

NPM install -g CNPM — registry.npm.taobao.org

Three yarn.

To compensate for NPM’s shortcomings, yarn is a new JS package management tool from Facebook, Google, Exponent and Tilde

So let’s look at the advantages of YARN:

1. Yarn uses local cache. Unlike NPM, YARN does not require an Internet connection to install local cache dependencies and provides offline mode.

2. In parallel installation, Yarn synchronously executes all tasks to improve performance.

3. Unified installation versions: Yarn has a lock file that records the version number of the module to be installed to prevent different versions from being pulled. Each time a module is added, Yarn creates (or updates) the yarn.lock file. This ensures that the same module version is used each time the same project dependency is pulled.

4. The output is concise and not as redundant as NPM

5. Multiple registry source processing: All dependencies, no matter how many times they are referenced indirectly by different libraries, will be installed from only one registry source

Contrast the Yarn and NPM commands

So I think you can understand the difference between the three. Next time your colleague asks you, “It works on your computer, but it doesn’t work on mine,” please kindly answer them. Hahaha

That’s it. Welcome to point out any questions