How to Clear Cache in NPM?

📋 Table Of Content
Here, in this article, we will learn how to clean or remove 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 just 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.
Now as we install a lot of packages to work on different projects, it piles up a lot of cache in our file system which 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, in 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. We can use the --force
or -f
flag to force clear the cache from your local system.
However, sometimes just the npm cache clean
command doesn't work, so the better way is to run:
npm cache clean --force
OR
npm cache clean -f
The clean command will remove all the data present in the cache folder.
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
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.
Is it safe to remove the npm cache folder?
Yes, it is completely safe to delete the cache folder. Next time when you install any npm package, the data will automatically get cached in the cache folder again.
Related Topics: