Getting started with the yarn package manager

Time:2019-12-14

Getting started with the yarn package manager

brief introduction

Yarn is a JavaScript package manager, a direct competitor of NPM, and one of Facebook’s open source projects.

It is compatible with NPM package, so it has a huge advantage as a direct replacement of NPM.

Because of parallel downloading and caching, it started much faster than NPM when it was first launched. While NPM is catching up with many of its features, yarn is still the solution I recommend.

The relationship between yarn and NPM is like a different download software. To download the same resource, use different download tools.

When tools develop to a certain extent, they will gradually converge in function and use experience. So competition is good for our users, NPM will also learn from and implement other good features.

Install Yarn

Although you can install yarn using NPM (NPM install – G yarn), it is not recommended by the yarn team.

The system specific installation method is https://yarnpkg.com/zh-hans/d. For example, on Mac OS, you can use homebrew and run:

Getting started with the yarn package manager

Each operating system has its own package manager, which makes the installation process very simple and smooth.

Finally, you get the yarn command available in the shell:

Getting started with the yarn package manager

Management package

Yarn writes its dependencies to a file called package.json, which is located in the root folder of the project, and stores the dependency files in the node [module] folder, just like NPM (if you’ve used it before).

Initialize a new project

Getting started with the yarn package manager

Install dependencies for existing projects
If you already have a package.json file that contains a list of dependencies, but the package has not been installed, run

yarn

perhaps

yarn install

To start the installation process.

Local local installation package

Install the package in the local project using:

yarn add package-name

Global installation package

yarn global add package-name

Install packages locally as development dependencies

yarn add --dev package-name

Equivalent to — save dev or – D in NPM

Remove package

yarn remove package-name

Check license

When you install many dependencies, there may be many other dependencies. You need to install many packages, and you know nothing about the licenses used by these packages.

Yarn provides a convenient tool to print licenses for any dependencies you have

yarn licenses list

Getting started with the yarn package manager

It also automatically generates a disclaimer that includes all licenses in your project:

yarn licenses generate-disclaimer

Getting started with the yarn package manager

Check dependencies

Have you checked the node? Modules folder and wondered why a specific package was installed?yarn whyThe command will tell you:

yarn why package-name

Getting started with the yarn package manager

Upgrade dependency package

If you want to upgrade a single package, run

yarn upgrade package-name

To upgrade all packages, run

yarn upgrade

However, this command sometimes leads to problems. Blind upgrade without considering the changes of the main version may lead to compilation and running failure of the project. The update of a large version of a software package is likely to affect the use of existing code.

Yarn has a good tool for you to selectively update packages in your project, which is very helpful for the last question:

yarn upgrade-interactive --latest

Getting started with the yarn package manager

–Latest: this flag tells yarn to ignore the version range specified by package.json and use the version marked in the latest registry

How to upgrade yarn

If you use brew to install it, as described above, use:

brew upgrade yarn

Or use NPM:

npm uninstall yarn -g
npm install yarn -g

For Windows users, please refer to the document for installation and upgrade: https://yarnpkg.com/zh-hans/d

Getting started with the yarn package manager

Getting started with the yarn package manager