Yarn introduction

Yarn is a JavaScript package manager and a direct competitor of npm, which is one of Facebook's open source projects.

Yarn introduction

Yarn isJavaScriptPackage manager, direct competitornpm, This is one of Facebook's open source projects.

it isCompatible with npm packages, So it has a huge advantage to replace npm.

It starts up much faster than beforenpmDue to parallel downloading and caching, but nownpmIt has indeed caught up with many of its features, and it is now my recommended solution.

Tools will eventually merge into a set of features to keep them at the same level to maintain relevance, so we may see these features in npm in the future-competition is good for our users.

Install yarn

Although you can install Yarn bynpm(npm install -g yarn), the Yarn team does not recommend this.

The system-specific installation methods are listed in the following locationshttps://yarnpkg.com/zh-CN/docs/install. For example, on macOS, you can useHouse wineAnd run

brew install yarn

But each operating system has its own package manager of choice, which can make the process very smooth.

Finally, you will getyarnCommands available in Shell:

Yarn command installed

Management Pack

Yarn writes its dependencies to a file namedpackage.json, Which is located in the root folder of the project, and stores the dependency file tonode_modulesfolder,Just likenpmIf you have used it in the past.

Initialize a new project

yarn init

Start an interactive prompt to help you quickly start a project:

Running yarn init

Install the dependencies of an existing project

If you already have onepackage.jsonFile containing a list of dependencies, but the package has not been installed yet, please run



yarn install

Start the installation process.

Install the package locally

Use the following command to install the package into the project

yarn add package-name

Install packages globally

yarn global add package-name

Install the package locally as a development dependency

yarn add --dev package-name

Equivalent to--save-devMark in npm

Take out the package

yarn remove package-name

Check the license

When installing many dependencies, these dependencies may have many dependencies. You will install many packages without knowing anything about the license used by these packages.

Yarn provides a convenient tool to print the licenses of any dependencies you have:

yarn licenses ls

The yarn packages licenses visualized

And it can also automatically generate a disclaimer, includingAllLicense for the project you are using:

yarn licenses generate-disclaimer

Disclaimer generated by yarn

Check dependencies

Have you checkednode_modulesFolder, want to know why a particular package is installed?yarn whyTell you:

yarn why package-name

The result of yarn why

Upgrade package

If you want to upgrade a single package, run

yarn upgrade package-name

To upgrade all packages, run

yarn upgrade

But this command can sometimes cause problems because you are blindly upgrading all dependencies without worrying about major version changes.

Yarn has a good tool to selectively update the packages in the project, which is of great help in this situation:

yarn upgrade-interactive

Upgrading packages with yarn

How to upgrade yarn

At the time of writing, there is no automatic update command.

If you have usedbrewInstall as suggested above, please use:

brew upgrade yarn

If you installed using npm, please use:

npm uninstall yarn -g
npm install yarn -g

More devtools tutorials: