How to Clear Cache in NPM?


Here, in this article, we will learn how to clean or clear the cache from npm from Windows, Linux, and Mac.

What is npm?

npm (node package manager) is used when we need to install node packages to set up the development environment for a project locally in our system.

While working on a web project we do need to install lots of node packages using npm install <package name> command.

Why do we need npm cache?

Whenever the packages are installed in a project, it also stores the data as .tar file in the cache folder of our system.

So, the next time you try to install the same package for a project, npm will extract the .tar file from the cache folder to the project directory.

This optimizes the installation speed of the package and also reduces internet usage.

As we install many packages to work on different projects, it piles up a lot of cache in our file system that needs to be cleared as it may hamper our development with lots of node errors.

By default, the cache is stored in the%AppData%/npm-cache directory in Windows and in Linux and mac the cache is stored in~/.npm .

Clearing npm cache

To clear npm cache, we need to run npm cache clean command in our terminal. It will delete the local cache of the npm packages manager. It will remove the outdated cache folders without impacting the performance of future operations.

So to delete the outdated cache except the recent one we can use the :

npm cache clean

Force clean npm cache

However, sometimes the npm cache clean command doesn’t work properly and you might still face some errors while installing packages. Then we can use the --force or -f flags with the clean command.

The --force or -f flags clean the entire npm cache from the system. It will delete all packages including the recent ones that would normally be kept.

To force clean npm package type :

npm cache clean --force

OR

npm cache clean -f

This command will remove all the data present in the cache folder including the latest caches.

The --force flag is very useful if you think your cache has become corrupted and want to wipe the cache to restore the functionality. The only downside to it is that you have to re-download all cached packages on next usage.

To verify if all the cache is cleared and removed from your file system, you can run this command in your terminal.

npm cache verify

So in summary we can say that:

npm cache clean: Cleans old cached packages, keeps the latest cache and

npm cache clean --force OR npm cache clean -f: Force deletes the entire npm cache including latest packages

It is recommended to use the npm cache clean command but if you need a fresh cache state then use the npm cache clean -f command.


FAQ

How do I force npm to clear cache?

To force npm to clear cache we need to run npm cache clean --force or npm cache clean --f.

What is the use of the npm cache verify?

The npm cache verify command checks the content of the cache folder and verifies the integrity of the cached data and its index and it also does the garbage collection of unwanted data.

Why clearing npm cache necessary?

Some of the key reasons why clearing your npm cache is necessary are:

  1. Errors Fixes – A corrupted cache can sometimes cause errors or bugs in npm packages. Clearing out the cache can resolve issues.
  2. Removed Unused Package: Cleaning your npm cache can remove all the unused packages accumulated in the cache. It will free up disk space too.
  3. Get the latest version of the package: An outdated cache may have an older package version. So cleaning it forces npm to re-download the latest version.
  4. Solve Installation issues: Sometimes old and corrupt packages can cause installation errors. So, clearing your npm cache can resolve such issues.

Is it safe to remove the npm cache folder?

Yes, it is completely safe to delete the cache folder. Next time you install any npm package, the data will automatically get cached in the cache folder again.


Related Topics:

Best Practices to yarn remove package

Fixed Error – npm ERR! missing script: dev

(Fixed) npm ERR! missing script: start error

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top