Dung (Donny) Nguyen

Senior Software Engineer

Manage Yarn Versions

There are several ways to manage the version of Yarn you are using, both globally and within specific projects. Here’s a breakdown of the common methods:

This is the preferred method for ensuring consistency across your project team. It allows you to specify the exact Yarn version that should be used within a project.

yarn set version <version>

Replace <version> with the desired Yarn version. Here are some examples:

When you run this command, Yarn will:

This ensures that anyone working on the project will use the exact same Yarn version, regardless of their global installation.

2. Using Corepack (Requires Node.js >= 14.19.0)

Corepack is a tool included with Node.js that allows you to manage package manager versions (npm, pnpm, and Yarn) on a per-project basis, based on the packageManager field in package.json.

  1. Enable Corepack globally (if you haven’t already):
    corepack enable
    
  2. In your project’s package.json, add or modify the packageManager field:
    {
      "name": "my-project",
      "version": "1.0.0",
      "packageManager": "yarn@3.6.4"
    }
    

    Replace "yarn@3.6.4" with the desired Yarn version.

Now, when anyone runs a Yarn command within that project directory, Corepack will automatically ensure that the specified version of Yarn is used. If that version isn’t already installed, Corepack will download and use it.

You can install or update Yarn globally on your system. However, this doesn’t guarantee that all your projects will use the same version.

4. Using Yarn Version Manager (yvm) - Deprecated

yvm was a tool to manage multiple Yarn versions, similar to nvm for Node.js. However, it is now deprecated in favor of Corepack. While you might still find it in older tutorials, it’s recommended to use Corepack for managing Yarn versions.

Checking Your Yarn Version

You can always check the currently active Yarn version by running:

yarn --version
# or
yarn -v

In summary, for managing Yarn versions effectively, especially for ensuring consistency within projects, the yarn set version command or using Corepack are the recommended approaches. Global installations can be useful for your personal development environment but don’t enforce version consistency across projects.