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
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
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"
}
3: Set up yarn config
$ yarn config set <key> <value>
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
or
$ yarn install
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
yarn berry
1: Install coreapack and then enable it
$ npm i -g corepack
$ corepack enable
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>
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",
}
3: Set up yarn config
$ yarn config set <name> <value>
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
This will create/update .yarnrc.yml file in the local directory, and the rule becomes a local config.
nodeLinker: node_modules
4: Initialize yarn in a new project
$ yarn init
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"
}
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
or
$ yarn install
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
Top comments (0)