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
- Getting Started with Svelte
- Create an RSS Reader in Node
- How to deploy a PHP app using Docker
- How to deploy an Express app using Docker
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- Using Puppeteer and Jest for End-to-End Testing
- Getting Started with Handlebars.js
- Getting Started with Moment.js
- Setting Up Stylus CSS Preprocessor
- Using Axios to Pull Data from a REST API