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
- Getting Started with Solid
- Managing PHP Dependencies with Composer
- How to deploy a .NET app using Docker
- How to deploy a PHP app using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Creating a Twitter bot with Node.js
- Getting Started with React
- Getting Started with Vuex: Managing State in Vue
- Setting Up a Local Web Server using Node.js