How to Clear the Cache in NPM
Table of Contents
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:
BASHnpm cache verify
The output looks something like this:
BASHCache 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:
BASHnpm cache clean --force
After this command is run, the cache folder will be empty. You can verify this by running the verify command again:
BASHnpm cache verify
BASHCache 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
- Managing PHP Dependencies with Composer
- Git Tutorial: Learn how to use Version Control
- How to deploy a .NET app using Docker
- Best Visual Studio Code Extensions for 2022
- How to build a Discord bot using TypeScript
- How to deploy a Deno app using Docker
- Getting Started with Sass
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Creating a Twitter bot with Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up Stylus CSS Preprocessor