How to migrate from Yarn Classic to NPM


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

  1. yarn <command> becomes npm run <command>
  2. Add -- when passing arguments to commands
  3. Replace yarn with npx 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)

Examples



Please support this site and join our Discord!