DEV Community

Cover image for How to Migrate from Classic Yarn to 'Modern Yarn' Without Losing Your Mind
Noelia
Noelia

Posted on

How to Migrate from Classic Yarn to 'Modern Yarn' Without Losing Your Mind

I spent most of my afternoon dealing with yarn versioning.
So here's the deal: Yarn used to be installed globally via npm i -g yarn or tools like brew or choco. Every project you worked on would use that global installation to manage dependencies, and it would usually install version 1, aka "classic.". Also the dreaded 1.22.1 (at least for me).
The issue with that is, if you updated yarn in the version 1 branch, old projects could break because of compatibility problems. Also, another afternoon for me on a different project. Those afternoons that could’ve been better spent drinking coffee. ☕️

Enter "Modern yarn" – starting from version 2 and now at version 4. The cool thing with this version is that it's installed per project via Corepack (a Node tool that handles different versions). This means each project can use its own version of yarn, which is great for avoiding compatibility issues. But to make this work, you’ve got to uninstall yarn globally and reinstall it using Corepack.
More info about Corepack here.

Here’s how to remove classic yarn in steps:

Step 1️⃣:
Uninstall Classic Yarn

Note: uninstall yarn based on how it was originally installed:

On macOS with Homebrew:

$ brew uninstall yarn
Enter fullscreen mode Exit fullscreen mode

If it was installed via npm:

$ npm remove yarn --global
Enter fullscreen mode Exit fullscreen mode

Step 2️⃣: Check Uninstallation 👀

Make sure yarn is no longer installed globally by checking the version:

$ yarn --version
Enter fullscreen mode Exit fullscreen mode

You should get something like this after it has been properly uninstalled.

Image description

If after uninstalling you still get the previous version, try this:

# On Mac:
$ which yarn

# On Win:
$ where yarn

# which/where will tell you, if and where yarn is installed. You get paths. Remove them!

$ rm -rf /usr/local/bin/yarn # use the path from before
$ rm -rf /usr/local/bin/yarnpkg # use the path from before

Enter fullscreen mode Exit fullscreen mode

Step 3️⃣: Corepack 👀
Install and enable corepack

Now install Corepack, if it is not available on your machine. And because it's still expertimental, enable it afterwards.

How to check if you have Corepack?

Image description

If you don't have it installed then:

$ npm install corepack --global
Enter fullscreen mode Exit fullscreen mode

Enable Corepack

$ corepack enable
Enter fullscreen mode Exit fullscreen mode

Now, use modern Yarn in your project (folder):

$ cd projects/my-project # choose your path
$ yarn set version stable
$ yarn install
Enter fullscreen mode Exit fullscreen mode

Switch a project

If you want to migrate a project to modern yarn, try this:

$ cd projects/my-project # choose your path
$ yarn set version stable
Enter fullscreen mode Exit fullscreen mode

or via corepack:

$ corepack use yarn@latest
Enter fullscreen mode Exit fullscreen mode

You could even install "Modern yarn" in a new version globally, if you want:

$ corepack install --global yarn@latest
Enter fullscreen mode Exit fullscreen mode

Personally, after doing all of this, I was still having issues 🤯 (the version remain 1.22.1). This is what worked for me:

Image description

But what's Yarn Berry?

Yarn Berry is a package management system for Node.js, created by Mäl Nison, the main developer of Yarn v1. The official version (v2) has been released since January 25, 2020, and is now being adopted by large open source repositories such as Babel. Yarn Berry is managed by source code in the GitHub repository.
This is a post by @solleedata explaining Yarn Berry in more detail, from which the description above is copied.

Top comments (0)