In this tutorial, we will learn about how to force yarn to reinstall a package using 5 best ways. The Node.js users must have felt the pain when see this error. You might have tried one or other solution to reinstall a package as it is the need of your code but you are stuck . I have written this article to help all such users by providing 5 best ways to resolve the error in the upcoming sections of this tutorial. So let’s get started.
Force Yarn to Reinstall a Package [5 Best Ways]
1. Delete ‘node_modules’ and ‘yarn.lock’
The very first method you can try is, delete the ‘node_modules’ and ‘yarn.lock‘ file in your project folder. This will delete all the installed packages. Hence yarn will be forced to reinstall all the packages when we run yarn install command. You can also use the –force flag during yarn installation. This will force yarn to install all the packages as shown below.
yarn install //to force reinstall yarn install --force
2. Clear Yarn Cache
Sometimes cache creates issues so delete yarn’s global cache by running below command. This will remove all cached packages. After that reinstall the package.
yarn cache clean yarn add <package-name>
3. Use yarn add Flag
If the issue is still seen, try to reinstall the package with —force flag . This will force yarn to reinstall the package as shown below.
yarn add <package-name> --force
4. Use –check-files Flag
You can also try to install yarn with –check-files flag. This flag force the yarn to check all files and reinstall the package if required.
yum install --check-files
5. Use Yarn Upgrade
If issue still persist, run yarn upgrade command. This command will install the latest version of the package. If you are trying to reinstall a package with specific version, you can skip this solution as it by default install the package to its latest version and update the same version in yarn.lock file as well.
yarn upgrade <package-name>
We have successfully resolved the error using one of the ways in this tutorial.