Skip to content

Instantly share code, notes, and snippets.

@favna
Last active February 18, 2023 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save favna/a4797720f60323241202ef1efa58c612 to your computer and use it in GitHub Desktop.
Save favna/a4797720f60323241202ef1efa58c612 to your computer and use it in GitHub Desktop.
Switch to yarn V3 copypasta dump

Yarn v3 is new version of Yarn that we recommend switching to as Yarn v1 has been deprecated.

"But I don't see any update on [source]?"

That is correct. Yarn v3 is installed through Yarn itself. Your global toolchain is and will always remain to be Yarn v1, however, you configure Yarn v3 on a per-project basis. How? Simply write:

yarn set version berry

This will download the new Yarn v3 binary and put in .yarn/releases, you should push this to your Git repository. It will also create a .yarnrc.yml file which configures the path which you should also commit.

Next you probably also want to run the following 2 commands:

yarn config set enableGlobalCache true
yarn config set nodeLinker node-modules

This will add to your .yarnrc file:

enableGlobalCache: true
nodeLinker: node-modules

Which ensures that you just have a Yarn v1-like experience with node_modules and a global cache.

Next step is to nuke your node_modules, yarn.lock, and if they exist package-lock.json (npm) and pnpm-lock.yaml (pnpm)

Now run yarn install to rebuild your node_modules with Yarn.

Then some final adjustments. Put this in you .gitignore:

# Yarn files
.yarn/install-state.gz
.yarn/build-state.yml

And anywhere in your scripts in package.json where you use * you should wrap it in extra " For example:

{
	"format": "prettier --write \"src/**/*.ts\""
}

Mind you this last thing is good add regardless of script runner / package bundler because it ensures the glob is performed by the library and not by your shell, which may differ when people develop on different operating systems.

In short the command to set everything up you can run:

yarn set version berry && yarn config set enableGlobalCache true && yarn config set nodeLinker node-modules && echo "" >> .gitignore && echo "# Yarn files" >> .gitignore && echo ".yarn/install-state.gz" >> .gitignore && echo ".yarn/build-state.yml" >> .gitignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment