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.
Once you have Node.js installed, proceed to Step 1 to install the Yarn package manager.
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 exact 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
Now that you have the
If you are using Yarn to work with an existing Yarn-based project, you can skip this step. 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
- 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...
Saving it into /home/sammy/my-project/.yarn/releases/yarn-berry.cjs...
Now try the
yarn --version command again:
- yarn --version
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.11 in this case. Every time you run
yarn, you are using the globally installed version of the command. 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
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 out 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 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
- yarn install
This will download and install the modules you need to get started.
add subcommand to add new dependencies to a project:
- yarn add package-name
This will download the module, install it, and update your
Yarn 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:
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
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.
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.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.