DEV Community

Arshak Grigoryan
Arshak Grigoryan

Posted on

Yarn classic vs Yarn berry installation guide

In this guide, we will learn how to create a new project using Yarn or how to add Yarn to an existing project.

There are two variants of yarn: classic and berry

Yarn classic refers to version 1.x. It is currently in legacy mode and receives only bug fixes.

Yarn berry refers to versions 2 and above. The current major version is 4.x, and it is actively maintained.

yarn classic

1: Install yarn

$ npm install -g yarn
Enter fullscreen mode Exit fullscreen mode

As the yarn classic versions are available in npm, we can install them via npm. Note: if you have corepack installed and enabled, you should disable it by running corepack disable before installing yarn globally.

2: Initialize yarn in a new/existing project

$ yarn init -y
Enter fullscreen mode Exit fullscreen mode

For convenience, we add a flag -y to skip the interactive form for creating new projects. This will create/update a package.json file with basic fields, and now we can edit it as we want.

{
  "name": "my-yarn-classic-project",
  "version": "1.0.0",
  "main": "index.js",
  "author": "arshak-grigoryan <arshak.code@gmail.com>",
  "license": "MIT"
}
Enter fullscreen mode Exit fullscreen mode

3: Set up yarn config

$ yarn config set <key> <value>
Enter fullscreen mode Exit fullscreen mode

By default, this command modifies the global config. So if we want to set it on the project level, we need to create a local file .yarnrc and edit it manually.

4: Install dependencies

For new projects, this will create a yarn.lock file.

$ yarn
Enter fullscreen mode Exit fullscreen mode

or

$ yarn install
Enter fullscreen mode Exit fullscreen mode

For the projects that have a yarn.lock file, this command does not generate a lockfile and fails if an update is needed.

$ yarn install --frozen-lockfile
Enter fullscreen mode Exit fullscreen mode

yarn berry

1: Install coreapack and then enable it

$ npm i -g corepack
$ corepack enable
Enter fullscreen mode Exit fullscreen mode

You probably can have an OS package manager like homebrew for macOS, and Node Version Manager, like nvm It is important to install corepack in npm global scope, and not with homebrew because it will use a separate node version rather than the one you use with nvm

2: Install yarn

$ yarn set version <version>
Enter fullscreen mode Exit fullscreen mode

set version supports concrete versions as well as specific tags, but it is better to install a stable version for new projects by running yarn set version stable. This will set the packageManager field in the package.json file.

{
  "packageManager": "yarn@4.12.0",
}
Enter fullscreen mode Exit fullscreen mode

3: Set up yarn config

$ yarn config set <name> <value>
Enter fullscreen mode Exit fullscreen mode

For example, by default, yarn uses PnP installation strategy, but we can change it to the traditional node_modules approach.

$ yarn config set nodeLinker node_modules
Enter fullscreen mode Exit fullscreen mode

This will create/update .yarnrc.yml file in the local directory, and the rule becomes a local config.

nodeLinker: node_modules
Enter fullscreen mode Exit fullscreen mode

4: Initialize yarn in a new project

$ yarn init
Enter fullscreen mode Exit fullscreen mode

This creates/updates package.json file, adds other files like yarn.lock, .gitignore,.gitattributes, and .yarn directory with its files that are git-ignored

{
  "name": "my-yarn-berry-project",
  "packageManager": "yarn@4.12.0"
}
Enter fullscreen mode Exit fullscreen mode

4: Add yarn to the existing project

Don’t use yarn init command for these purposes, as it will rewrite existing package.json by removing all scripts and dependencies. Set yarn config as you want, and then install dependencies by running yarn

5: Install dependencies

For new projects, this will create a yarn.lock file.

$ yarn
Enter fullscreen mode Exit fullscreen mode

or

$ yarn install
Enter fullscreen mode Exit fullscreen mode

For the projects that have a yarn.lock file, this command does not generate a lockfile and aborts with an error exit code if an update is needed.

$ yarn install --immutable
Enter fullscreen mode Exit fullscreen mode

Top comments (0)