I. Guidelines on the official website

Official website: Yarn official website

Chinese document: Yarn Chinese document

2. Yarn Introduction

Yarn is a new JavaScript package management tool developed by Facebook, Google, Exponent and Tilde. You can use code from developers all over the world, or share your own code. Code is shared through packages, or modules. A package contains all the code that needs to be shared, along with a file that describes the package information, called package.json. It has the advantage of being faster, safer and more reliable. Key features include offline mode, deterministic, network performance, multiple registrations, network recovery, flat mode, and Emoji.

3. Advantages of Yarn

** Fast: **Yarn caches every 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 a detailed and concise lock file format and a clear installation algorithm, Yarn is guaranteed to work equally on different systems.

** Security: ** Yarn uses algorithms to verify the integrity of each installation package before executing the code.

Install Yarn in Windows

[1] Download the installation package and install it

Click me to download the Yarn installation package and you will download an.msi

File that, when run, directs you to install Yarn on Windows. If you use this installer, you will need to install Node.js first.

[2] Install via Chocolatey

Chocolatey is a software package management tool for Windows. Please follow these instructions to install Chocolatey. After Chocolatey is installed, you can install Yarn from the console by executing the following commands

choco install yarn
Copy the code

[3] Installation through Scoop

Scoop is a command line based installation tool for Windows. Follow these instructions to install Scoop. Once Scoop is installed, you can install Yarn from the console by executing the following commands

scoop install yarn
Copy the code

5. Install Yarn in Linux

[1] The official website script performs one-click installation

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

[2] Install through NPM

npm install -g yarn
Copy the code

[3] Check the version after the installation is successful

yarn --version
Copy the code

6. Yarn common commands

[1] Initialize the new project

yarn init
Copy the code

[2] Add dependency packages

Yarn add [package] // The latest version is automatically installed. Yarn Add [package] [package] [package] // Add multiple packages at a time yarn add [package]@[version] // Add the package of the specified version yarn add [package]@[tag] // Install a tag (such as beta,next or latest)Copy the code

[3] Add dependencies to different dependency categories

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

Yarn add [package] --dev or yarn add [package] -d // Add to devDependencies yarn add [package] --peer or yarn add [package] -p Yarn add [package] --optional or yarn add [package] -o add to optionalDependenciesCopy the code

[4] Upgrade 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 tagCopy the code

[5] Remove dependency packages

Yarn Remove [package] // Remove the packageCopy the code

[6] Install package dependencies in 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 // Forcibly download all packages YARN Install again -- Production // Only install production environment dependenciesCopy the code

[7] Release packages

yarn publish
Copy the code

[8] Run the script

Yarn run // Used to execute scripts defined under the scripts property in package.jsonCopy the code

[9] Display information about a packet

Yarn Info [package] // You can view the latest version information about a moduleCopy the code

[10] Cache

Yarn cache yarn cache list // Lists each packet that has been cached yarn cache dir // Returns the global cache location yarn cache clean // Clears the cacheCopy the code

7. Compare the yarn and NPM commands

NPM Yarn instructions
npm init yarn init Initialize a project
npm install/link yarn install/link Default installation dependencies
npm install taco –save yarn add taco Install a dependency and save it by default to the Package
npm uninstall taco –save yarn remove taco Remove a dependency
npm install taco –save -dev yarn add taco -dev Install a development-time dependency
npm update taco –save yarn upgrade taco Update a dependent project
npm install taco –global yarn global add taco Install a global dependency project
npm publish/login/logout yarn publish/login/logout Publish/login/exit
npm run/test yarn run/test Run a command

The article is updated every week. You can search “Front-end highlights” on wechat to read it in the first time, and reply to [Books] to get 200G video materials and 30 PDF books

​​