js package management tool yarn

 

Yarn is another javascript package management tool similar to npm.

Its purpose is to solve a few problems faced when using npm, namely:

1. Speed/consistency cannot be guaranteed during installation

2. Security issues, because npm allows code to run during installation

 

One: install yarn

1: Installation command

npm install -g yarn

2: View the yarn version

yarn --version

Two: initialization

1: Enter the project directory to execute

	
yarn init


2: A package.json will be generated in the root directory, similar to npm, no specific explanation

 

Three: add dependency

1. Add package: yarn add [pkg-name], the latest version will be installed automatically, and the specified version number will be overwritten

For example to add axios:

yarn add -g axios

After the installation is complete, we will find the axios folder under ./node_modules:

13131313.png

 

2: Add multiple packages at once

yarn add -g jquery qs
# 添加jquery以及qs

3: Add the package of the specified version: yarn add [pkg-name]@ver

yarn add [email protected]
#添加 2.1.4版本的jquery

 

4: Update the package to the specified version: yarn upgrade [pkg-name]@ver

yarn upgrade [email protected]
# 将 jquery从2.1.4更新到3.0.0版本

5: Update the package to the latest version: yarn upgrade --latest [pkg-name]

yarn upgrade --latest jquery

6: Delete the package: yarn remove [pkg-name]

yarn remove jquery

7: Delete multiple packages at once: yarn remove [pkg-name1] [pkg-name2]

yarn remove bootstrap zepto

Four, yarn.lock automatically locks the installation package version

Npm has a feature called shrinkwrap, whose purpose is to lock package dependencies when used in a production environment. The challenge of shrinkwrap is that every developer must manually run npm shrinkwrap to generate the npm-shrinkwrap.json file.

With Yarn, it is completely different. During the installation process, a yarn.lock file will be automatically generated, and yarn.lock will record all the large and small installations you have installed. It's a bit similar to composer.lock, which is familiar to PHP developers. yarn.lock locks the exact version of the installation package and all dependencies. As long as you don't delete the yarn.lock file, when you run yarn install again, you will get all the dependency packages based on the version number recorded in it. With this file, you can be sure that each member of the project team has installed the exact package version, the deployment can be easily reproduced, and there are no unexpected bugs. You can submit yarn.lock to this library, so that when you check out the code and run yarn install, you can ensure that the dependencies you install are exactly the same.

For example, the dependencies installed above will be recorded in yarn.lock, as shown below:

141414.png

 

Five: comparison of yarn and npm commands

npm install

yarn add

(N/A)

yarn add --flat

(N/A)

yarn add --har

npm install --no-package-lock

yarn add --no-lockfile

(N/A)

yarn add --pure-lockfile

npm install [package] --save

yarn add [package]

npm install [package] --save-dev

yarn add [package] --dev

(N/A)

yarn add [package] --peer

npm install [package] --save-optional

yarn add [package] --optional

npm install [package] --save-exact

yarn add [package] --exact

(N/A)

yarn add [package] --tilde

npm install [package] --global

yarn global add [package]

npm update --global                    

yarn global upgrade                      

npm rebuild

yarn add --force

npm uninstall [package]

yarn remove [package]

npm cache clean

yarn cache clean [package]

rm -rf node_modules && npm install  

yarn upgrade                            

npm version major                      

yarn version --major                    

npm version minor                      

yarn version --minor                    

npm version patch                      

yarn version --patch                    

 

The above command is copied from yarn official website.

For more details, please refer to the official yarn document: https://yarn.bootcss.com/docs/install/#windows-stable

 

Have a good suggestion, please enter your comment below.

 

Welcome to personal blog: https://guanchao.site

Welcome to the Mini Program:

 

 

Guess you like

Origin blog.csdn.net/qq_39708228/article/details/112987620