The Basics of Using Yarn
In this section, we are going to explain the essentials of Yarn, including how to start a new project, manage dependencies, and update the package manager.
Starting a New Project
Here’s how to start a new project using Yarn:
- Create a new project by running the following command:
yarn init project_name
The output will show the current version of the Yarn package manager.
Press Enter. You will be presented with eight consecutive configuration questions.
If you want Yarn to use the default values, press Enter after each question.
Yarn will create two configuration files – package.json and yarn.lock. The former contains all the dependencies you have set when answering the questions, while the latter locks them to specific versions to ensure consistency within the dependency tree.
Managing Dependencies
Adding a new dependency to your project will automatically download the module, install it, and update the package.json and yarn.lock files. Run the following command to do it:
yarn add [package]
It is also possible to define a specific version for each package or library as a dependency for your project. For example:
yarn add [package]@[version]
To update a package of projects specifically managed with Yarn, use the upgrade directive with the package name. For example:
yarn upgrade [package]
In addition, to specify which version to use in order to update the package, use this command:
yarn upgrade [package]@[version]
To remove a package from a project, run the command below:
yarn remove [package]
Running the above command will automatically update the package.json and yarn.lock files of the project.
Finally, to install all the defined dependencies, run the command below:
yarn install
Keep in mind that these dependencies are defined in the package.json file.
Upgrading Yarn
There are four methods to upgrade Yarn. Before doing it, verify the version you’re running. Do it by running the following command:
yarn --version
In order to upgrade Yarn, run either of these commands:
- If Yarn was installed using npm:
npm install --global yarn
- To update Yarn to the latest version:
yarn set version latest
- To update Yarn to a specific version:
yarn set version [version.number]
- For Unix machines using cURL:
curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

