This post goes over how to migrate from Yarn Classic (v1) to NPM.
Prerequisites
NPM 9+ is installed:
npm --version
If you’re on an older version, you can upgrade to the latest version:
npm install --global npm@latest
Lockfile
Delete yarn.lock
and node_modules
:
rm -rf yarn.lock node_modules
Install and create package-lock.json
:
npm install
Scripts
If your package.json
“scripts” use yarn
:
{
"scripts": {
"lint": "eslint .",
"lint:fix": "yarn lint --fix"
}
}
Then replace yarn
with npm
:
{
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
}
}
Note
yarn <command>
becomesnpm run <command>
- Add
--
when passing arguments to commands - Replace
yarn
withnpx
when executing a binary
Engines
If your package.json
“engines” has yarn
:
{
"engines": {
"yarn": "1"
}
}
Then replace yarn
with npm
:
{
"engines": {
"npm": "9"
}
}
Files
Search for all files that contain yarn
:
grep -nr 'yarn' .
Or search inside a Git repository:
git grep 'yarn'
Then refactor yarn
to npm
. For example:
- Documentation (e.g., README.md)
- Scripts (e.g., Git hooks)
- CI (e.g., GitHub Actions)