Migrating to Yarn 2 (also known as Yarn Berry) from an earlier version like Yarn 1 involves some changes to your project's setup and dependencies. One of the main goals of the migration is to simplify dependency management and improve performance. During the migration, you may encounter peer dependency issues, as Yarn 2 handles them differently than Yarn 1.
Here's a step-by-step guide on how to migrate to Yarn 2 and address peer dependency issues:
Install Yarn 2: Install Yarn 2 globally on your system using npm:bash
npm install -g yarn@berry
Initialize Yarn 2 Project: Navigate to your project directory and initialize a Yarn 2 project:bash
This will create a new
.yarndirectory and a
Migrate Dependencies: Replace your
node_modulesfolder and existing
yarn.lockfile with the ones generated by Yarn 2:bash
rm -rf node_modules mv yarn.lock .yarn/
Install Dependencies with Yarn 2: Use Yarn 2 to install your project dependencies:bash
Yarn 2 will automatically handle peer dependencies differently, which can help in resolving peer dependency issues.
Update Scripts in package.json: If you have any custom scripts in your
package.json, you might need to adjust them to work with Yarn 2. Yarn 2 introduces the concept of "plugins," so certain commands might need modification. For example, change
"build": "react-scripts build"to
"build": "yarn run react-scripts build".
Yarn Plug'n'Play: Yarn 2 introduces Plug'n'Play (PnP) which replaces the traditional
node_modulesfolder. With PnP, the dependencies are linked directly from the
.pnp.jsfile, which can lead to some incompatibilities with certain packages. If you encounter issues, refer to the Yarn 2 documentation on how to troubleshoot PnP-related problems.
Adjust for Workspace Setup (Optional): If you have a Yarn workspace setup (using a
workspacesfield in your
package.json), Yarn 2 has a different workspace configuration. Refer to the Yarn 2 documentation for workspace setup guidelines.
Check Compatibility: Ensure that all your project's dependencies are compatible with Yarn 2. Some older or less maintained packages might not work well with Yarn 2, so you may need to look for alternatives or updates.
Testing and Verification: Thoroughly test your application after the migration. Check that all dependencies are resolved correctly, and your application works as expected. Be prepared to debug and fix any issues that might arise during the migration.
Update Documentation and CI/CD: Update your project's documentation and any CI/CD configurations to reflect the changes made during the migration.
Remember that Yarn 2 introduces some breaking changes compared to Yarn 1, so it's essential to understand the differences and adjust your project accordingly. Always take backups and use version control to avoid any permanent damage during the migration process. Refer to the official Yarn 2 documentation for more details and insights into the migration process and new features.