99

Concurrent visitors over the last 30 minutes. (~)

Peak concurrent visitors -
Yesterday: 0 / Today: 0

Installing and Using Yarn

Installing and Using Yarn

Let's try Yarn, the JavaScript package manager created by Meta (formerly Facebook).

# Installing Yarn

You can install Yarn on a variety of operating systems.

# macOS

Install it with Homebrew:

BASH
1
brew install yarn

If you manage Node.js with a version manager such as NVM, leave out the Node.js install:

BASH
1
brew install yarn --without-node

# Windows

Install it with Chocolatey:

BASH
1
choco install yarn

Or install it with Scoop:

BASH
1
scoop install yarn

# npm

You can also install it with npm.

BASH
1
npm install -g yarn

Check that the installation worked:

BASH
1
yarn --version

If you can't run it globally after installing, you need to add it to your PATH.

Add the following to .profile, .bash_profile, .bashrc, .zshrc, or a similar file:

BASH
1
export PATH="$PATH:/opt/yarn-[version]/bin"

# Using Yarn

If you've used npm before, this will feel familiar.

To initialize a new project (this creates package.json):

BASH
1
yarn init

To install the dependencies listed in package.json:

BASH
1
2
3
yarn # or yarn install

To add a dependency:

BASH
1
2
3
yarn add [package] yarn add [package]@[version] yarn add [package]@[tag]

To add a dependency to another category, such as devDependencies, peerDependencies, or optionalDependencies:

BASH
1
2
3
yarn add [package] --dev yarn add [package] --peer yarn add [package] --optional

To upgrade a dependency:

BASH
1
2
3
yarn upgrade [package] yarn upgrade [package]@[version] yarn upgrade [package]@[tag]

To remove a dependency:

BASH
1
yarn remove [package]

# yarn.lock

The yarn.lock file records the versions of the installed modules, so you get dependencies with the same versions and structure on every machine.
Yarn generates yarn.lock automatically every time you run yarn install.
It serves the same purpose as npm's package-lock.json.