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 TypeScript
How to Install Node on Windows, macOS and Linux
Getting Started with Solid
Managing PHP Dependencies with Composer
Getting Started with Svelte
Create an RSS Reader in Node
How to Serve Static Files with Nginx and Docker
Best Visual Studio Code Extensions for 2022
How to deploy a PHP app using Docker
How to deploy a Deno app using Docker
How to deploy a MySQL Server using Docker
Getting User Location using JavaScript's Geolocation API
