3 min read · May 2, 2023
--
Migrating from npm to Yarn is a fairly easy process for most users, as Yarn can consume the same package.json format as npm, and can install any package from the npm registry.
Here are the steps to migrate npm to Yarn:
- Install Yarn globally on your machine by running the command
npm i -g yarn
. - Go to the directory where you installed packages and run the
yarn
command. This will lay out your node_modules folder using Yarn’s resolution algorithm that is compatible with the Node.js module resolution algorithm. - Yarn will generate a
yarn.lock
file within the root directory of your package. You don’t need to read or understand this file - just check it into source control. When other people start using Yarn instead of npm, theyarn.lock
file will ensure that they get precisely the same dependencies as you have. - In your
package.json
file, replace all npm commands in "scripts" with yarn commands. In some cases, the information in a package.json file is not explicit enough to eliminate dependencies, and the deterministic way that Yarn chooses dependencies will run into dependency conflicts. This is especially likely to happen in larger projects where sometimes npm install does not work and developers…