From Yarn to Pnpm
Yarn and Pnpm are both package managers.
Why?
In order to choose between these two alternatives, we have checked the following criteria:
- popularity: this is the package manager that is used by all the major frameworks (NextJS, VueJS, Vite, etc.)
- performance : pnpm uses hardlinks for
node_modules
by default, so you won't end up with 100GB ofnode_modules
across the projects on your machine. This also makes performance way better for installation, update, etc. - stability: the way pnpm handles workspaces (monorepo) and
node_modules
in each package is much better than yarn: you have a node_modules folder in all your packages, rather than everything in the root. The impact is that if you have a dependency that does something crazy (like Serverless that dynamically requires modules), it will work
For more context, check out the migration PR.
How?
Migrating from yarn to pnpm is quite straightforward:
-
install pnpm
npm install -g pnpm
-
rename all your
yarn
commands topnpm
:yarn
->pnpm install
yarn test
->pnpm test
yarn package
->pnpm package
yarn deploy
->pnpm run deploy
(run is required here, aspnpm deploy
is a reserved command)
-
replace all occurrences of the string
yarn.lock
in your source files withpnpm-lock.yaml
(search, prettier, etc.) -
in your CI/CD, when using
actions/setup-node@v3
, setcache
to'pnpm'
-
if you're using yarn PnP, remove
.yarnrc.yml
and the.yarn
folder -
in the root
package.json
set thepackageManager
key topnpm@<version>
(replace<version>
with the latest available version) -
create a
pnpm-workspace.yaml
file with:packages:
- 'services/*'
- 'contracts/*'
- 'packages/*'and everything that is in the
workspaces
key of the rootpackage.json
-
run
pnpm import
to generate apnpm-lock.yaml
from youryarn.lock
, then removeyarn.lock
-
run
pnpm install
Troubleshooting
Pnpm is more strict than Yarn when dealing with dependencies. This is a good thing, because it makes package configurations self-sufficient.
However, migrating from yarn to pnpm, you may find that you miss some dependencies in your packages for this reason. In this case:
- add the missing types packages for obvious ones
- dig into dependencies to find out source issues in less obvious ones
Most often it should be an issue with peer dependencies that are not properly satisfied.