Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

Writing in the front

Yarn is a JavaScript package management tool (alternative to NPM). Safe, Stable, Reproducible Projects has been quoted on Yarn’s website.

As described on the Yarn official website, Yarn has the advantages of high speed, security, and reliability. Compared with NPM, Yarn has many optimized functions, such as network performance optimization and the same dependency installation mode. For details, see Yarn’s Chinese website.

The installation of the Yarn

To install Yarn, run the NPM command if you have installed Node.js. The command is as follows:

Check whether node.js is present
node-v
# installation of yarn
npm install -g yarn

Copy the code

After the installation is complete, run the following command to check whether the installation is successful:

yarn -v
Copy the code

If the version is displayed, the installation is complete. If the version is 1.x. x, the installation is successful

Set the image source of the YARN library as follows:

yarn config set npmRegistryServer https://registry.npm.taobao.org
Copy the code

Common Yarn commands

Initialize the

yarn init

Copy the code

Adding a dependency package

yarn add [package] # Install the latest version automatically, overwriting the specified version number
yarn add [package] [package] [package] Add multiple packages at once
yarn add [package]@[version] Add the specified version of the package
yarn add [package]@[tag] # Install a tag (such as Beta, Next or latest)

Copy the code

Add dependencies to different dependency categories

You can specify dependencies to add to devDependencies, peerDependencies, and optionalDependencies, respectively.

# 加到 devDependencies
yarn add [package] --dev
# or
yarn add [package] -D

Add to peerDependencies #
yarn add [package] --peer
# or
yarn add [package] -P

Add to optionalDependencies #
yarn add [package] --optional
# or
yarn add [package] -O

Copy the code

Upgrading dependency packages

yarn upgrade [package] # Upgrade to the latest version
yarn upgrade [package]@[version] Upgrade to the specified version
yarn upgrade [package]@[tag] # upgrade to the specified tag

Copy the code

Removing dependency packages

yarn remove [package] # remove packages

Copy the code

Install dependencies from package.json and save dependencies into yarn.lock

yarn Install all dependencies
yarn install Install all dependencies
yarn install --flat Install a single version of a package
yarn install --force # Force all packages to be redownloaded
yarn install --production Install only production environment dependencies

Copy the code

Publish a package

yarn publish

Copy the code

Run the script

yarn run Json to execute scripts defined under the scripts property in package.json

Copy the code

Displays information about a package

yarn info [package] You can view the latest version of a module

Copy the code

The cache

yarn cache
yarn cache list List each package that has been cached
yarn cache dir Return the global cache location
yarn cache clean # clear cache

Copy the code