How to Clear the Cache in NPM

How to Clear the Cache in NPM

NPM is a package manager for Node and when you install packages using npm install (package-name), the files and data are saved in the cache folder.

When you try installing a package you already have, it will not download the files again but instead use the files from the cache.

The problem is that NPM will not remove unneeded data by itself and the cache folder size will grow over time. In this post, we will learn how to clear the cache of NPM.

Verify the Cache

Before you clear the cache, you should verify the cache folder size to see how much data is in the cache. To do this, run the following command in your terminal:

npm cache verify

The output looks something like this:

Cache verified and compressed (~\AppData\Local\npm-cache\_cacache)
Content verified: 4037 (304348519 bytes)
Content garbage-collected: 1086 (1278374746 bytes)
Missing content: 1
Index entries: 4039
Finished in 24.918s

As you can see, my cache folder is over 300MB in size.

Clear the Cache

Now that you know how much data is in the cache, you can clear the cache. To clear the cache in NPM, we need to run the cache clean command:

npm cache clean --force

After this command is run, the cache folder will be empty. You can verify this by running the verify command again:

npm cache verify
Cache verified and compressed (~\AppData\Local\npm-cache\_cacache)
Content verified: 0 (0 bytes)
Index entries: 0
Finished in 0.006s

Conclusion

NPM uses a cache to speed up the installation process and to save data that is not needed. Since NPM does not automatically remove unneeded data, the cache folder size will grow over time. This is why clearing the cache is useful. Hopefully, you have learned how to clear the cache of NPM.

Resources

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.