Yarn installation and use tutorial

Posted by dallasx on Mon, 10 Jan 2022 16:10:01 +0100

2, Introduction to Yarn

Yarn is a new JavaScript package management tool developed by Facebook, Google, Exponent and Tilde. You can use the code of developers all over the world or share your own code through it. Code is shared through packages (or module s). A package contains all the code to be shared and a file describing the package information, which is called package json. Its advantages are faster, safer and more reliable. Its main features are offline mode, certainty, network performance, multi registration, network recovery ability, flat mode and Emoji.

3, Advantages of Yarn

Fast: Yan caches each downloaded package, so there is no need to download it again. Parallel downloads are also used to maximize resource utilization, so installation is faster.

Reliable: using detailed and concise lock file format and clear installation algorithm, Yan can ensure no difference in work on different systems.

Security: before executing the code, Yan will verify the integrity of each installation package through an algorithm.

4, window system installation

[1] Download installation package

Click me to download the Yarn installation package , you will download to a msi file, which will guide you to install Yarn on Windows when it runs. If you use this installer, you need to install it first Node.js.

[2] Install via chocolate

Chocolatey Is a special software package management tool for Windows. Please follow this explain Install chocolate. After installing chocolate, you can execute the following commands on the console to install Yan

choco install yarn

[3] Install via Scoop

Scoop Is a command line based installation tool for Windows. Please follow this explain Install Scoop. After snoop is installed, you can execute the following commands on the console to install Yan

scoop install yarn

5, Linux system installation Yarn

[1] The official website script executes one click installation

curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly

[2] Installation via npm

npm install -g yarn

[3] View version after successful installation

yarn --version

6, Yarn common commands

[1] Initialize new project

yarn init

[2] Add dependent package

yarn add [package] // The latest version is automatically installed and the specified version number is overwritten
yarn add [package] [package] [package] // Add multiple packages at once
yarn add [package]@[version] // Adds a package of the specified version
yarn add [package]@[tag] // Install a tag (such as beta,next or latest)

[3] Add dependencies to different dependency categories

Do not specify the dependency type, which is installed in dependencies by default. You can also specify the dependency type to add to devDependencies, peer dependencies and optional dependencies respectively

yarn add [package] --dev or yarn add [package] -D // Add to devdependences
yarn add [package] --peer or yarn add [package] -P // Add to peer dependencies
yarn add [package] --optional or yarn add [package] -O // Add to optional dependencies

[4] Upgrade dependent package

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

[5] Remove dependent packages

yarn remove [package] // Remove package

[6] Install package JSON, and save the package and all its dependencies into yarn lock

yarn or yarn install // Install all dependencies
yarn install --flat // Install a single version of a package
yarn install --force // Force all packages to be re downloaded
yarn install --production // Install only production environment dependencies

[7] Release package

yarn publish

[8] Run script

yarn run // Used to execute in package Script defined under the scripts attribute in JSON

[9] Displays information about a package

yarn info [package] // It can be used to view the latest version information of a module

[10] Cache

yarn cache
yarn cache list // Lists each cached package
yarn cache dir // Returns the global cache location
yarn cache clean // Clear cache