How To Clear Cache In NPM?
To clear the cache in npm, run this command:
npm clear cache
In this article, I will explain different ways of clearing the cache in npm, why cache errors happen, how does the cache work, and many more.
Let's get to it 😎.
How does the npm cache work?
Installing a new npm dependency will automatically store a corresponding tar file inside the npm cache folder. If you install the same package again, this time, it won't be downloaded from the internet but taken from the npm cache.
The npm cache significantly reduces your downloads and optimizes the dependencies' installation time.
Note: Npm's cache verifies all the data's integrity on insertion and extraction.
Why clear your npm cache?
Since the fifth version of npm, clearing the cache is, in most cases, unnecessary since npm's cache is resistant to corruption and can self-heal. However, they are still some valid reasons to clear your cache.
Here are some of them:
- To fix the "Please run npm cache clean" error.
- To fix libraries that didn't download correctly.
- If you desire to reinstall all the packages freshly.
- To fix libraries that didn't save correctly.
- If you wish to clear up disk space.
How to clear cache?
To clear cache in npm, type:
npm clear cache
This command removes all the data inside the cache directory.
Sometimes, however, this command will not work, and you will need to use the force option:
npm clear cache --force
If you want to verify that the command has successfully cleared the cache, use the npm cache verify command:
npm cache verify
This command runs an offline verification of the cache's content.
As you can see, clearing a cache in npm is super easy and can be done using one command. However, it should be unnecessary to clear the cache in most cases since the new npm version handles cache corruption very well.
Here is another guide that I wrote (if you are interested in learning more about npm and other tools):