Skip to content

How to clear cache in npm

Jul 14, 2022Abhishek EH3 Min Read
How to clear cache in npm

NPM uses caching mechanism to avoid downloading the files from the internet when you have run npm install <package_name> previously and install the same package again. In this article, we will see how to display the cache size and location, the reasons to clear the npm cache, and how to clear the npm cache.

Viewing your npm cache

You can see the cache size and location by running the following command in the terminal:

1npm cache verify

When I run the above command, I get the following results:

1Cache verified and compressed (~\AppData\Local\npm-cache\_cacache)
2Content verified: 22305 (2605126906 bytes)
3Content garbage-collected: 17036 (7999814602 bytes)
4Index entries: 24134
5Finished in 231.569s

The above command will take some time to complete. As you can see in my case it took 231 seconds.

As you can see the location of the npm cache in my machine is ~\AppData\Local\npm-cache\_cacache. It may change in your machine. You can go to that directory and see the contents.

As you can see above, it will also print the size of the cache. In my case, it is 2605126906 bytes, which translates to 2.6 GB 😲.

Reasons to clear npm cache

One of the obvious reasons is to save some space in your machine. The other reason is to fix any corrupt packages.

As of npm v5, any corruption in npm cache is auto healed by npm.

Clearing npm cache

You can try running the following command to clear the cache:

1npm cache clean

If you are having npm version 5 or later, then you will get the following error:

1npm ERR! As of npm@5, the npm cache self-heals from corruption issues
2npm ERR! by treating integrity mismatches as cache misses. As a result,
3npm ERR! data extracted from the cache is guaranteed to be valid. If you
4npm ERR! want to make sure everything is consistent, use `npm cache verify`
5npm ERR! instead. Deleting the cache can only make npm go slower, and is
6npm ERR! not likely to correct any problems you may be encountering!
7npm ERR!
8npm ERR! On the other hand, if you're debugging an issue with the installer,
9npm ERR! or race conditions that depend on the timing of writing to an empty
10npm ERR! cache, you can use `npm install --cache /tmp/empty-cache` to use a
11npm ERR! temporary cache instead of nuking the actual one.
12npm ERR!
13npm ERR! If you're sure you want to delete the entire cache, rerun this command
14npm ERR! with --force.

You can force npm to clear the cache with the following command:

1npm cache clean --force

Once the command is run successfully, you can run npm cache verify to confirm that the cache is cleared.

1Cache verified and compressed (~\AppData\Local\npm-cache\_cacache)
2Content verified: 0 (0 bytes)
3Index entries: 0
4Finished in 0.009s

Disadvantages of clearing npm cache

The main disadvantage of clearing npm cache is it will slow down installing packages since it has to download the packages from the internet even though the package was installed previously.

If you have liked article, do follow me on twitter to get more real time updates!

Leave a Comment

© 2024 CodingDeft.Com