How to install and use Yarn Package Manager for Node.js

0 Shares
0
0
0
0

Introduction

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

Some of Yarn's unique features include:

  • Storage mechanism in each project, which can greatly speed up subsequent installation and construction.
  • Fixed and definitive installations that ensure the structure of installed libraries is always the same.
  • Testing the aggregation of all packages to verify their 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 you can install and use the Yarn Package Manager, you must have Node.js installed. To check if you already have Node.js installed, type the following command in your local command line terminal:

node -v

If you see a version number printed like v12.16.3, you have Node.js installed. If you encounter a command not found error (or similar), please install Node.js before continuing.

Step 1 – Global YARN Installation

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

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

sudo npm install -g yarn

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

yarn --version
Output
1.22.11

Now that you have installed the yarn command 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 should already be set up with a local version of Yarn and all the configuration files necessary to use it.

If you are starting a new project for yourself, you will now want to configure a project-specific version of Yarn.

First, go to your project directory:

cd ~/my-project

If you don't have a project directory, you can create a new folder with mkdir and then enter it:

mkdir my-project
cd my-project

Now from the command yarn set To set the version of Berry use:

yarn set version berry

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

Output
Resolving 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
Output
3.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 set up with a project-specific version of Yarn. Next, we'll look at some commonly used Yarn commands to get you started.

Using Yarn

Yarn has a lot of 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 its online help. In Yarn, you can add the --help flag to any command to get more information:

yarn --help

This prints general help for the yarn command. To get more detailed information about a subcommand, add –help after the subcommand:

yarn install --help

This will print details about how to use the yarn installation command.

Starting a new YARN project

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

yarn init

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

Installing all dependencies for a project

To download and install all 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.

Adding a new dependency to a project

Use the add subcommand to add new dependencies to the project:

yarn add package-name

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

Updating file .gitignore For Yarn

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

.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*

This will ignore the entire yarn. directory and then add 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.

Result

In this tutorial, you installed Yarn and were introduced to a few yarn subcommands. For more information on using Yarn, take a look at the official Yarn CLI documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like