How To Install and Use the Yarn Package Manager for Node.js

Introduction

Yarn is a package manager for Node.js which focuses on speed, security, and consistency. It was originally created to address some issues with the popular NPM package manager. Although the two package managers have since converged in terms of performance and features, Yarn remains popular, especially in the world of React development.

Some of the unique features of Yarn are:

  • A per-project caching mechanism, which can greatly speed up subsequent
  • installations and builds

  • The consistent and deterministic installations that guarantee the structure of the installed libraries are always the same
  • Checksum check of

  • all packages for integrity
  • “Workspaces”, which facilitate the use of Yarn in a monorepo (multiple projects developed in a single source code repository)

In this tutorial you will install Yarn globally, add Yarn to a specific project, and learn some basic Yarn commands

.

Prerequisites

Before installing and using the Yarn package manager, you must have Node.js installed. To see if you already have Node.js installed, type the following command in your local command-line terminal:

  1. node -v

If you see a version number, such as v12.16.3 printed, you have Node.js installed. If you get a command not found error (or similar wording), install Node.js before proceeding.

To install Node.js, follow our tutorial for Ubuntu, Debian, CentOS or macOS.

Once you have installed Node.js, proceed to step 1 to install the Yarn package manager.

Step 1 — Installing

Yarn Globally

Yarn has a unique way to install and run in your JavaScript projects. First, install the yarn command globally, and then use the global yarn command to install a specific local version of Yarn in the project directory. This is necessary to ensure that everyone working on a project (and all automated project testing and deployment tools) is running the exact same thread version, to avoid inconsistent behaviors and results.

Yarn maintainers recommend installing Yarn globally using the NPM package manager, which is included by default with all installations .js Node. Use the -g flag with npm install to do this:

  1. sudo npm install -g

yarn

After installing the package, have the yarn command print its own version number. This will allow you to verify that it was installed correctly:

yarn -version Output1.22.11

Now that you have the yarn command installed globally, you can use it to install Yarn

in a specific JavaScript project.

Step 2 — Installing

Yarn in Your Project

If you are using Yarn to work with an existing Yarn-based project, you can skip this step. The project must already be configured with a local version of Yarn and all the configuration files required to use it.

If you’re setting up a new project of your own, you’ll want to set up a specific version of the Yarn project now.

First, navigate to your

project directory: cd ~/my-project

If you don’t have a project directory, you can create a new one with mkdir and then move on to it: mkdir

my-project

  1. cd my-project

Now use the yarn set command to set the version

to berry: yarn

  1. set version berry

This will download the current and actively developed version of Yarn – berry – save it in a .yarn/releases/ directory in your project, and also set up a .yarnrc.yml configuration file: OutputSolving berry in a

url … Downloading https://github.com/yarnpkg/berry/raw/master/packages/berry-cli/bin/berry.js… Saving it to /home/sammy/my-project/.yarn/releases/yarn-berry.cjs… Updating /home/sammy/my-project/.yarnrc.yml… Done! Now try the command yarn -version

again:

  1. yarn –

version Output3.0.0

You will see that the version is 3.0.0 or higher. This is the latest version of Yarn.

Your project is now configured with a specific version of the Yarn project. Below, we’ll look at some commonly used thread commands to get you started.

Using

Yarn

Yarn has many subcommands, but you only need a few to get started. Let’s look at the first subcommands you’ll want to use.

When

getting started with any new tool, it’s helpful to learn how to access their online help. In Yarn, the -help flag can be added to any command for more information:

yarn -help This will print the general help for the

  1. yarn

command

. For more specific information

about a subcommand, add -help after the subcommand: yarn install -help

This would print details about how to use the yarn install command.

Starting a new

thread project

If you’re starting a project from scratch, use the init subcommand to create the Yarn-specific files you’ll need:

  1. Thread initiation

This will add a package.json configuration file and a yarn.lock file to your directory. The package.json file contains the configuration and dependency list of the module. The yarn.lock file locks those dependencies to specific versions, ensuring that the dependency tree is always consistent.

Install all dependencies in a project To download and install

all dependencies for an existing Yarn-based project, use

the install: yarn install subcommand

This will download and install the modules you need to get started.

Add a new dependency to a project Use the add subcommand to add

new dependencies

to a project:

  1. yarn
  1. add package-name

This will download the module, install it, and update its package.json and yarn.lock files

.

Update the .gitignore file for

Yarn

Yarn stores files in a .yarn folder within the project directory. Some of these files should be checked in version control and some should be ignored. The basic .gitignore configuration for Yarn is as follows: .yarn

/* !. thread/patches!. thread/releases!. Yarn/plugins !. thread/SDKs !. Yarn/.pnp versions.*

This ignores the entire .yarn directory, and then adds some exceptions for important folders, including the versions directory that contains the specific version of the Yarn project.

For more details on how to configure Git and Yarn, see the official Yarn documentation at .gitignore.

Conclusion

In this tutorial you installed Yarn and learned about some thread subcommands. For more information on using Yarn, see the official Yarn CLI documentation.

For more general help on Node.js and JavaScript,

visit our Node.js and JavaScript tag pages, where you’ll find relevant tutorials, tech talks, and community Q&A.

Contact US