npm

When using the NPM installation package, it seems slow at first, prints out a bunch of things on the command line, and often appears with “On my computer I can……” This question is really a little uncomfortable. But NPM is one of the main reasons why Node is so successful.

The disadvantages of NPM are as follows:

1. Due to the influence of the version number, it will cause the situation of inconsistent versions

NPM is designed around the idea of semantic versioning


Give a version number: the major version number. Secondary version number. Patch version number


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


Secondary version number: When added functionality, but backward compatible


Patch version number: When backward compatible defects were fixed

Json dependency: “5.0.3”, // Install the specified version of 5.0.3″ ~5.0.3″, // Install the latest version of 5.0.x “^5.0.3” // Install the latest version of 5.0.x

So different developers using the same package.json file may have different versions of the same library installed on their own machines, which can lead to potentially hard-to-debug bugs and “on my computer…” In the case.

2. Because the NPM inventory is nested with dependencies, it will increase the probability that the corresponding version cannot be matched. For each package, there will be nested other dependent packages.

When installing, packages will be downloaded and installed at the same time. At some point, a package fails, but NPM will continue to download and install packages. Because NPM prints all the logs to the terminal, the error information about the error package gets lost in a lot of npm-printed warnings, so it’s hard to find the actual error package and the cause.

Npm 2 will install all the dependencies of each package. For one project, A depends on B, and B depends on C. The dependency tree is as follows:

node_modules
- package-A
-- node_modules
--- package-B
----- node_modules
------ package-C
-------- some-really-really-really-long-file-name-in-package-c.js

This structure can be very long. This is a minor annoyance for UNIX-based operating systems, but it can be disruptive for Windows, because there are many programs that can’t handle file pathnames longer than 260 characters.

NPM 3 uses a flat dependency tree to solve this problem, so our three project structures now look like this:

node_modules
- package-A
- package-B
- package-C
-- some-file-name-in-package-c.js

In this way, A previously long file pathname changes from./node_modules/ package-a /node_modules/ package-b /node-modules/some-file-name-in-package-c.js to /node_module S/some – file – name – in – package – SAN Antonio s.

The downside of this approach is that NPM must first iterate through all of the project dependencies and then decide how to generate the flat node_modules directory structure. NPM must build a complete dependency tree for all the modules used, which is a time-consuming operation and a significant reason for the slow installation of NPM.

Also, because NPM is downloaded from a foreign server, the speed will be slow due to network problems.

cnpm

Compared to NPM, CNPM is much faster to download. CNPM is faster because Taobao first requests the content of foreign servers to its own domestic servers, so when we use CNPM, we rely on downloading from the domestic servers, which is much faster.

“This is the full npmjs.org image that you can use in place of the official version,” the website says. “Synchronizing is currently done every 10 minutes to ensure maximum sync with the official service.”

yarn

Advantages of YARN:

Yarn offline mode If you have installed a package before, you can reinstall it without any Internet connection.

YARN has a lock file that keeps track of the exact version of the installed module, and each time a new file is added, yarn creates (or updates) yarn.lock to ensure that the same version of the module is installed each time the dependencies are installed.

3. Flat mode reduces the different versions of dependent packages to a single version to avoid creating multiple copies.

Command comparison for NPM and yarn

npm yarn
npm install Yarn or yarn install
npm install react –save yarn add react
npm uninstall react –save yarn remove react
npm install react –save-dev yarn add react –dev
npm update –save yarn update

References:


Differences and contrasts between NPM and yarn


Differences between NPM, CNPM, and yarn


YARN Chinese documentation