Force Yarn to Reinstall a Package [5 Best Ways]

Force Yarn to Reinstall a Package [5 Best Ways] - linuxnasa

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.

 

Yarn Overview

Yarn is a package manager (just like yum, dnf, apt-get etc) used for JavaScript code. Yarn is an alternative and enhanced solution for npm(Node package Manager). ‘npm’ is a default package manager that comes with Node.js. Users prefer to use Yarn to install packages due to some shortcomings and performance issues observed in npm.

 

NOTE:

Please make sure you take the backup of yarn.lock file and node_modules beforing trying any of the solutions in this tutorial.

 

Force Yarn to Reinstall a Package [5 Best Ways] 

Also Read: Installing node.js on Linux [Step by Step Guide]

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

 

NOTE:

If you want to reinstall a package with specific version, modify the version in the package.json file. Then execute yum install command.

 

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>

 

Summary

We have successfully resolved the error using one of the ways in this tutorial.

Leave a Comment