In this article, you will learn everything you need to know to get started with Yarn.
There are many ways to install Yarn, and the easiest way to do it, if you were using npm before, it’s to install it from npm. Yes, you did hear right!
npm install --global yarn
By running this command, you can install Yarn globally. The other method is to follow the steps describe in the documentation for your operating system.
In both cases, don’t forget to set up the
PATH environment variable in your terminal to have access to Yarn’s binaries globally.
Finally, you will probably want to upgrade Yarn when new versions will be released. To update to the latest version, you can run:
Now we are good to go. Let’s see how Yarn works and how to use it in your projects.
Even if Yarn is an alternative to npm, it is compatible with both the npm and bower workflows and supports mixing registries. In other words, you can use Yarn in your existing npm projects.
Let’s see it in action. I have a project where I’m using React as a dependency. My project contains a
package.json file which describes it:
To install the dependency, you can run the
yarn command with no arguments. It will fetch the package from the npm registry based on the
package.json dependencies list. Like npm, it will populate the
After going further into the Yarn CLI commands, you have probably notice that Yarn creates and populates a lockfiles called
yarn.lock at the root of the project:
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
As written at the beginning of this file, do not edit this file directly. It is entirely handle by Yarn when using the Yarn CLI to update, remove, or add dependencies.
This file aims to solve issues from npm around versioning and non-determinism. It locks each installed dependency with its exact version to ensure consistency across all machines when installing the project. This is why you should include it into the source control repository of your project.
Yarn provides a rich set of command-line commands to help you with various aspects of your project, from creating a new project to managing dependencies and more. While the most popular commands are explained below, you can refer to the official documentation for advanced commands.
Creating a New Project
We’ve seen before that you can use Yarn with existing npm projects. In addition to that, you can also start a completely new project thanks to Yarn just by running the following command:
Like npm, you will have to answer the following questions when creating a new Yarn project. It’s up to you to answer them or leave them blank for default values.
The only thing you should get from the
yarn init command is a
package.json like this one:
"description": "My new Yarn project",
"author": "Grégory D'Angelo",
If you were used to work with npm, this file should be familiar to you. It contains all the info about your project.
Working with Dependencies
Now that we know how to create a new project with Yarn, let’s see how to manage dependencies.
The first thing to do it’s to add packages in our project. You can do it easily by running the command below.
yarn add [package-name]@[version-or-tag] [option]
In case you don’t specify the version or tag option, the latest version of the package will be installed. The option argument could be
--tilde. For the first three options, it will install the package in your
As you may have noticed in the example above, adding the React package is quite fast!
Yarn fetches the package from the npm registry to the
node_modules directory of your project. It also updates your
You could also add packages from other locations such as:
- a local folder or a local gzipped tarball
yarn add file:/path/to/local/folder
yarn add file:/path/to/local/tarball.tgz
- a remote git repository
yarn add <git remote url>
- a remote gzipped tarball
yarn add https://my-project.org/package.tgz
Finally, to upgrade or remove a packages, you just need to run the following CLI commands. It will upgrade all dependencies to their latest version or remove the corresponding package from your project. Both commands will update your
package.json and your
# upgrade all dependencies
# remove react
yarn remove react
For more advanced use cases with Yarn, please refer to the official documentation.
At the time of this writing, Yarn already has more than 16,000 stars on Github. The project looks very promising as it tries to solve common problems developers may encounter with other package managers. Since the project is open to the community, it will benefit from the help of others engineers to become even better.
Subscribe Now for Exclusive Content
Also published on Medium.