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:
- Enable
corepackif you haven't already
corepack enable
- 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)
- Add the following to your
.gitignorefile:
.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.*
- You'll also need to create a
.gitattributesfile 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.
- You can now install dependencies with Yarn 4:
yarn install