Overview

Yarn is a fast, reliable, and secure dependency management tool for JavaScript. Developed by Facebook, Yarn was created to address some of the shortcomings of NPM, such as speed and consistency in package installations.

Yarn uses a lockfile to ensure that dependencies are installed consistently across different environments.

Installation

There are multiple ways to install Yarn. In this guide, we will show the three easiest methods for installing Yarn. You can find information on other methods here.


Open your terminal and run this command

npm install -g yarn

The above command will install Yarn

Run this command to verify if Yarn is installed

  yarn -v

It should show something like 1.21.0

Usage

Initializing a Project

yarn init

This command initializes a new project and creates a package.json file.


Installing a Package

yarn add <package_name>

This command installs a package locally in your project and updates the package.json and yarn.lock files.

Example

yarn add lodash

This installs the lodash package, a utility library for JavaScript.


Installing a Package Globally

yarn global add <package_name>

This command installs a package globally on your system, making it available from any directory.

Example

yarn global add create-react-app

This installs create-react-app, a tool for creating React applications, globally.


Updating a Package

yarn upgrade <package_name>

This command updates a package to the latest version. If no package name is specified, it updates all project dependencies.

Example

yarn upgrade lodash

This updates the lodash package to its latest version.


Uninstalling a Package

yarn remove <package_name>

This command removes a package from your project and updates the package.json and yarn.lock files accordingly.

Example

yarn remove lodash

This uninstalls the lodash package.