How to Use Yarn instead of NPM

1. About yarn

Yarn is a new JS package management tool jointly launched by Facebook, Google, exponent and tilde. As written in the official document, yarn appears to make up for some defects of NPM

2. Yarn advantage

1. Fast

The fast speed mainly comes from the following two aspects:

1.1 parallel installation: both NPM and yarn will perform a series of tasks when installing packages. NPM is to execute each package according to the queue, that is to say, it must wait until the current package installation is completed before the subsequent installation can continue. Yarn performs all tasks synchronously, which improves performance

1.2. Offline mode: if a software package has been installed before and is retrieved from the cache when using yarn to install it again, you don’t need to download it from the network like NPM

2. Unified installation version

In order to prevent pulling different versions, yarn has a lock file that records the version number of the module exactly installed. Each time a new module is added, yarn will create (or update) the yarn. Lock file. This ensures that every time you pull the same project dependency, you use the same module version. NPM actually has a way to use the same version of packages everywhere, but the developer needs to execute the NPM shrinkwrap command. This command will generate a lock file. When NPM install is executed, the lock file will be read first. This is the same reason that yarn reads yarn.lock file. The difference between NPM and yarn is that yarn generates such a lock file by default, while NPM generates the npm-shrinkwrap.json file through the shrinkwrap command. Only when this file exists will the packages version information be recorded and updated

3. More concise output

The output information of NPM is lengthy. When NPM install is executed, the command line will print out all the installed dependencies. In contrast, yarn is too concise: by default, it combines Emoji to print out the necessary information intuitively and directly, and also provides some commands for developers to query for additional installation information

4. Multi registration source processing

All dependent packages, no matter how many times they are indirectly associated and referenced by different libraries, will only be installed from one registered source, either NPM or bower, to prevent confusion and inconsistency

5. Better semantics

Horn changes the names of some NPM commands, such as horn add/remove, which is clearer than the original NPM install/install

3.Yarn installation

npm install -g yarn

4.Yarn command

1. View version

yarn -v

2. Create project

yarn init

3. Installation dependency

yarn or yarn install

4. Run the script

yarn run 

5. Package build

yarn build

6. Display a package information

yarn info 

7. Lists the dependencies for the current project

yarn list

8. Displays the current configuration

yarn config list

9. Lists each package that has been cached

sudo yarn cache list 

10. Clear cache

sudo yarn cache clean

5. NPM Comparison

Npm Yarn
npm install yarn
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 upgrade

Similar Posts: