Skip to main content

Use Yarn 4

To enable Yarn 4 in a project, you'll need to activate it with corepack

Follow these steps to switch to Yarn 4:

  1. Enable corepack if you haven't already
corepack enable
  1. Set Yarn version to 4 (Berry)
yarn set version berry

This makes Yarn 4 the default package manager for the project (a packageManager field is added to your package.json)

  1. Add the following to your .gitignore file:
.gitignore
# Yarn 4
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you wish to use zero-installs
# In that case, don't forget to run `yarn config set enableGlobalCache false`!
# Documentation here: https://yarnpkg.com/features/caching#zero-installs

#!.yarn/cache
.pnp.*
  1. You'll also need to create a .gitattributes file with the following content:
.gitattributes
/.yarn/**            linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generateds
info

By default, this will still use node_modules for backwards compatibility. If you want to use PnP, you run:

yarn config set nodeLinker pnp

This creates a .yarnrc.yml file and adds the nodeLinker field to it.

  1. You can now install dependencies with Yarn 4:
yarn install