Yarn Batch Manager – How To

Пакетный менеджер Yarn - инструкция по применению

Hi!) Today we are going to take a look at the Yarn package manager. A bit of history. Yarn appeared as a development from Facebook. And when introduced, it was intended to replace the npm package manager. Since yarn was several times faster when installing packages. Today, after the release of the 5th version of npm, npm has significantly accelerated, and now in terms of speed it is quite comparable to yarn. Although yarn is faster, it is not critical.

Yarn works with the same packages as npm. And it solves the same tasks as npm. It only makes it a little faster. 30% – 50% when comparing yarn and npm after version 5.

Yan works with the same package.json file and installs packages in the node_modules directory. That is, it is a complete, replaceable analogue of npm.

Video overview of working with the Yarn package manager


How to install Yarn

There are two ways to install the yarn package manager.

First. In my opinion, it is faster and more convenient. If you are already using npm, you can quickly install yarn like a regular global npm package installation:

npm i yarn -g

Second. Install via the distribution kit from the official website, following the link: https://yarnpkg.com/en/docs/install

Verifying yarn installation

After yarn is installed, check. To do this, type in the console:

yarn -v

In response, you will see the version of yarn. In my case, the answer was 1.7.0

Getting Started with Yarn

A new project is initialized by the command:

yarn init

To quickly create a file and automatically answer all questions yes,
you can run the command with the flag -y

yarn init -y

After that, a package.json file will be created in the root of the project. It will be very slightly different than if you had built it with npm. But this is absolutely not critical. Since yarn and npm are completely interchangeable.

Installing packages from package.json

If you are working with a ready-made project and you need to install all dependencies from the packege.json file use the command:


Or yarn install

Yarn.lock file

After initialization, or installing packages with yarn, you will see a new file yarn.lock… Don’t delete it. This is the yarn technical file. Just store it in your project and add it to version control (Git for example). It records the versions of all packages and dependencies that you have installed. In order for your colleagues, who will be working with the same project, to install the same versions of the packages – the main ones and the dependencies. In npm, its analogue is the package-lock.json file.

For beginners, the question arises: “Do I need to upload a file to the Git repository yarn.lock or package-lock.json? ” Answer: It is necessary!

Basic commands for working with Yarn

Start of a new project

yarn init

Installing the package

yarn add [package] // установит пакет и запишет его в зависимости Dependencies
yarn add [package]@[version] // установит пакет определенной версии
yarn add [package]@[tag] // установит пакет определенной версии

Installing a package with an entry into other dependency categories

You can install the package in devDependencies, peerDependencies, and optionalDependencies :

yarn add [package] --dev
yarn add [package] --peer
yarn add [package] --optional

Dependency update

yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]

Remove dependency

yarn remove [package]

Install all project dependencies



yarn install

Offline installation of packages in Yarn

If you try to install a package with the internet disconnected, yarn will throw an error and prompt you to use the —offline flag.

This flag allows you to install packages that have already been installed and stored in the cache. For example, let’s install jquery:

yarn add jquery --offline

Comparison (analogs) of commands in NPM and Yarn

npm (v5) Yarn
npm init yarn init
npm init --yes yarn init -y
npm install yarn install
(no analogue) yarn install --flat
(no analogue) yarn install --har
npm install --no-package-lock yarn install --no-lockfile
(no analogue) yarn install --pure-lockfile
npm install [package] yarn add [package]
npm install [package] --save-dev yarn add [package] --dev
(no analogue) yarn add [package] --peer
npm install [package] --save-optional yarn add [package] --optional
npm install [package] --save-exact yarn add [package] --exact
(no analogue) yarn add [package] --tilde
npm install [package] --global yarn global add [package]
npm update --global yarn global upgrade
npm rebuild yarn add --force
npm uninstall [package] yarn remove [package]
npm cache clean yarn cache clean [package]
rm -rf node_modules && npm install yarn upgrade
npm version major yarn version --major
npm version minor yarn version --minor
npm version patch yarn version --patch

Related links:

Yarn documentation

Leave a Reply

Your email address will not be published.