Yarn is a package manager for Node.js that focuses on speed, security, and consistency. It was originally created to address some issues with the popular NPM package manager. Though 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:
In this tutorial, you will install Yarn globally, add Yarn to a specific project, and learn some basic Yarn commands.
Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
Before installing and using the Yarn package manager, you will need to have Node.js installed. To see if you already have Node.js installed, type the following command into your local command line terminal:
- 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 phrasing), please install Node.js before continuing.
To install Node.js, follow our tutorial for Ubuntu, Debian, CentOS, or macOS.
Once you have Node.js installed, proceed to Step 1 to install the Yarn package manager.
These steps are valid for the most recent versions of Ubuntu: Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04. If you are using Ubuntu version <=18.04, we recommend you upgrade to the latest version as Ubuntu no longer supports the older versions. This collection of guides will help you in upgrading your Ubuntu version.
Yarn has a unique way of installing and running itself in your JavaScript projects. First, you install the yarn
command globally, then you use the global yarn
command to install a specific local version of Yarn into your project directory. This is necessary to ensure that everybody working on a project (and all of the project’s automated testing and deployment tooling) is running the same version of yarn
, to avoid inconsistent behaviors and results.
The Yarn maintainers recommend installing Yarn globally by using the NPM package manager, which is included by default with all Node.js installations. Use the -g
flag with npm install
to do this:
- sudo npm install -g yarn
After the package installs, have the yarn
command print its own version number. This will let you verify it was installed properly:
- yarn --version
Output1.22.22
Now that you have the yarn
command installed globally, you can use it to install Yarn into a specific JavaScript project.
You can skip this step if you are using Yarn to work with an existing Yarn-based project. The project should already be set up with a local version of Yarn and all the configuration files necessary to use it.
If you are setting up a new project of your own, you’ll want to configure a project-specific version of Yarn now.
First, navigate to your project directory:
- cd ~/my-project
If you don’t have a project directory, you can make a new one with mkdir
and then move into it:
- mkdir my-project
- cd my-project
Now use the yarn set
command to set the version to berry
:
- yarn set version berry
This will download the current, actively developed version of Yarn – berry
– save it to a .yarn/releases/
directory in your project, and set up a .yarnrc.yml
configuration file as well:
OutputResolving berry to a url...
Downloading https://github.com/yarnpkg/berry/raw/master/packages/berry-cli/bin/berry.js...
Saving it into /home/sammy/my-project/.yarn/releases/yarn-berry.cjs...
Updating /home/sammy/my-project/.yarnrc.yml...
Done!
Now try the yarn --version
command again:
- yarn --version
Output4.5.0
You’ll see the version is 3.0.0
or higher. This is the latest release of Yarn.
Note: If you cd
out of your project directory and run yarn --version
again, you’ll once again get the global Yarn’s version number, 1.22.22
in this case. Every time you run yarn
, you use the command’s globally installed version. The global yarn
command first checks to see if it’s in a Yarn project directory with a .yarnrc.yml
file, and if it is, it hands the command off to the project-specific version of Yarn configured in the project’s yarnPath
setting.
Your project is now set up with a project-specific version of Yarn. Next, we’ll look at a few commonly used yarn
commands to get started with.
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 starting with any new tool, it’s useful to learn how to access its online help. In Yarn the --help
flag can be added to any command to get more information:
- yarn --help
This will print out overall help for the yarn
command. To get more specific information about a subcommand, add --help
after the subcommand:
- yarn install --help
This would print out details on how to use the yarn install
command.
If you’re starting a project from scratch, use the init
subcommand to create the Yarn-specific files you’ll need:
- yarn init
This will add a package.json
configuration file and a yarn.lock
file to your directory. The package.json
contains the configuration and your list of module dependencies. The yarn.lock
file locks those dependencies to specific versions, making sure that the dependency tree is always consistent.
To download and install all the dependencies in an existing Yarn-based project, use the install
subcommand:
- yarn install
This will download and install the modules you need to get started.
Use the add
subcommand to add new dependencies to a project:
- yarn add package-name
This will download the module, install it, and update your package.json
and yarn.lock
files.
.gitignore
File for YarnYarn stores files in a .yarn
folder inside your project directory. Some of these files should be checked into version control and others should be ignored. The basic .gitignore
configuration for Yarn follows:
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.*
This ignores the entire .yarn
directory and then adds in some exceptions for important folders, including the releases
directory which contains your project-specific version of Yarn.
For more details on how to configure Git and Yarn, please refer to the official Yarn documentation on .gitignore
.
In this tutorial, you installed Yarn and learned about a few yarn
subcommands. For more information on using Yarn, take a look at the official Yarn CLI documentation.
For more general Node.js and JavaScript help, please visit our Node.js and JavaScript tag pages, where you’ll find relevant tutorials, tech talks, and community Q&A.
DigitalOcean provides multiple options for deploying Node.js applications, from our simple, affordable virtual machines to our fully-managed App Platform offering. Easily host your Node.js application on DigitalOcean in seconds.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Can add a section about Yarn adding and managing global packages? I’m stuck and searching for resources.
use “yarn add” instead of “yarn install”