Skip to content

Instantly share code, notes, and snippets.

@macx
Last active June 21, 2024 05:38
Show Gist options
  • Save macx/21d444166d169f8eff09c0c2f3f0f523 to your computer and use it in GitHub Desktop.
Save macx/21d444166d169f8eff09c0c2f3f0f523 to your computer and use it in GitHub Desktop.
Switch from classic yarn to modern yarn

Switch from classic yarn to modern yarn

Why

Previously, yarn was installed globally via npm i -g yarn or brew install yarn/choco install yarn and every project you are working on uses it to handle it's dependencies. yarn itself will installed in version 1, which is called "classic". If you update yarn in the version 1 branch over time, old projects could become not compatible anymore.

Here is "Modern yarn" kicking in, because it will be installed not globally, it will installed per project with corepack which is a tool from Node to handle different versions. Modern yarn starts from version 2 and is now 4.

Installed per project basis, you can operate your projects with different yarn versions independently – a huge benefit in terms of compatibiliy. But in order to so, you have to remove yarn globally and reinstall it with corepack.

Remove the classic yarn

First, uninstall classic yarn by the way you installed it (brew, choco or npm).

# on MacOS with Homebrew
$ brew uninstall yarn

# on Windows with Chocolatey
$ choco uninstall yarn

# if it was installed via Node
$ npm remove yarn --global

If this doesn't help, try this:

# On Mac:
$ which yarn

# On Windows:
$ 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

Install and enable corepack

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

$ npm install corepack --global
$ corepack enable

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

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

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

# or via corepack
$ corepack use yarn@latest

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

$ corepack install --global yarn@latest

See: https://yarnpkg.com/corepack, https://stackoverflow.com/questions/42334978/how-do-i-uninstall-yarn, https://yarnpkg.com/getting-started/install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment