The npm cache is essentially a local storage area where npm retains downloaded packages and their dependencies.
Its primary goal is to aid package installations by reducing the need for constant internet downloads.
However, over time, the npm cache can become a source of issues.
In this guide, we will look at why clearing the npm cache is necessary and how to clear it.
There are a few reasons why you might want to clear the npm cache:
PS:
If you encounter an error like this –
npm ERR! code EINTEGRITY
, you can read on how to fix it: How to Fix npm Installation Error: npm ERR! code EINTEGRITY?
Here are steps to follow in order to clear the npm cache:
Start by verifying the integrity of the npm cache.
This command checks for corrupted files and ensures the cache is in a healthy state.
In order words, it helps to verify the cache’s integrity and reports whether any files needs repair.
npm cache verify
In the case of the image above, the output indicates that the cache verification process is completed successfully without finding any corrupted files.
After verifying the cache and there is an error, proceed to clear it with this command:
npm cache clean --force
Even if there are no errors, but you still want to clear cache, then just run the command.
This command forcefully removes all files from the cache, including any outdated package versions.
The --force
flag ensures that the operation proceeds without requiring additional confirmation.
In rare cases, you may encounter issues when attempting to clear the npm cache using the npm cache clean
command.
If that happens, you can resort to manually deleting the cache folder.
The cache is located in different directories depending on your operating system:
~/.npm
%AppData%/npm-cache
For example, for window users, you can press the “Win+R” key on the keyboard to launch the “Run” program.
Type in %AppData%/npm-cache
and click ok. This should bring forth the npm cache folder and then you can delete.