How to clean npm cache
How to clean npm cache
Why and how to clear npm cache
Posted on Jun 30, 2021
Learn why you may need to clear your npm cache and how to do it
Your npm cache files will be stored under
/.npm/_cacache folder by default.
With cached files, npm will optimize the number of downloads and internet usage for you.
But since npm won’t remove data by itself, the cache size will only grow as you install new packages to your local computer.
For example, after 9 months of using a new laptop, I found my npm cache size has grown to 705 MB.
This is why you may want to clear your npm cache files: you want to clean up packages that you know you’re not going to require anymore, including older versions of your dependencies, and reclaim your disk space.
Now before cleaning your cache, you may want to verify the cache with npm cache verify command first to see how big the cache size is:
Note that the verification process might take quite a long time depending on the size of your cache.
The cache clean command will look for the package.json file in the current directory where you run the command.
At first, I thought it will only clear the cache from dependencies listed in a package.json file, but after running the command, I found all cached files have been removed completely.
Once the cache clean command finished running, you can run npm verify cache again to look at the cache size.
The following shows my verified cache content has been cleared completely:
And that’s how you can clear the npm cache 😉
For further information about npm cache, you can see the documentation here.
Conclusion
The npm cache system provides you with a way to save internet data usage by saving the package data and reuse it when you install the same package again.
Since npm version 5, the npm cache system has been improved so that it can self-heal and resist data corruption issues.
This way, you won’t have to clear your cache for any corrupted data issues. The only reason you want to clear your cache is because the size has grown so big over a long period of time, so you need to reclaim the disk space.
Level up your programming skills
I’m sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Nathan Sebhastian is a software engineer with a passion for writing tech tutorials.
Learn JavaScript and other web development technology concepts through easy-to-understand explanations written in plain English.
How to clear your cache in npm
If you ever get weird errors in npm like Please run npm cache clean you may need to clean or refresh your npm cache.
Run: “npm cache verify” for npm version 5 and up
However if you’re running npm v5 and above, npm is supposed to be self-healing, and you may get the following error message when trying to clean the npm cache:
Run: “npm cache clean –force”
This will force delete the npm cache on your computer.
What does npm cache clean do?
When you install npm packages on your computer, npm will first add the packages and dependencies in your local npm cache folder. This will be
/.npm on Posix, or %AppData%/npm-cache on Windows, according to the npm documentation.
Then npm will install the packages into the local project’s node_modules folder.
Then, in the future, if you install any of the same packages, npm will install them from your cache directly instead of having to download them from npm again.
However, sometimes the cache can get corrupted if different versions of dependencies are installed and conflict with one another. Clearing the cache will let you install the packages from npm again and have a fresh start.
I’m making a course that will teach you how to build a real-world responsive website from scratch!
Sign up to get emails about new posts and other info. Unsubscribe anytime.
Affiliate Disclaimer
I participate in various affiliate programs and my content contains affiliate links. If you purchase through those links, I may receive a commission from the seller, at no cost to yourself. It’s one way you can support this site!
As an Amazon Associate I earn from qualifying purchases. I only recommend products that I personally know and believe are helpful to my readers.
Please run `npm cache clean`
npm ERR! tar.unpack error reading /media/FLASH/Tech/IPFS/ipfs
npm ERR! addLocal Could not install /media/FLASH/Tech/IPFS/ipfs
npm ERR! Linux 4.15.0-29-generic
npm ERR! argv «/usr/local/bin/node» «/usr/local/bin/npm» «install» «ipfs» «—save»
npm ERR! node v6.9.2
npm ERR! npm v3.10.9
npm ERR! 0-byte tarball
npm ERR! Please run npm cache clean
I don’t know why I’m getting this error. and how to avoid this error.
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If you’re sure you want to delete the entire cache, rerun:
This error can be due to many many things.
Another reason why it cannot read could be because it has not downloaded correctly, or saved correctly. A little problem in your network could have caused that, and the cache clean would remove the files and force a refetch but that does not solve your problem. That means it would be more on the save part, maybe it didn’t save because of permissions, maybe it didn’t not save correctly because it was lacking disk space.
NPM clean modules
Is there a way to get npm to unbuild all the modules under node_modules? Something like npm rebuild that removes all build artifacts but doesn’t rebuild them?
9 Answers 9
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can just delete the node_module directory
There is actually special command for this job
It will delete node_modules directory and will install packages with respect your package-lock.json file
I added this to my package.json:
Seems to work well.
it will find all node_modules and let you remove them.
You can take advantage of the ‘npm cache’ command which downloads the package tarball and unpacks it into the npm cache directory.
The source can then be copied in.
Using ideas gleaned from https://groups.google.com/forum/?fromgroups=#!topic/npm-/mwLuZZkHkfU I came up with the following node script. No warranties, YMMV, etcetera.
For windows environment:
I have added few lines inside package.json:
npm ci works for this scenario, but only when your package.json and package-lock.json are in sync, which might not always be the case if you have been working on either one to resolve conflicts quickly or are updating on the directory level by removing directories/symbolic links. A comprehensive answer to the question would be this..
Not the answer you’re looking for? Browse other questions tagged node.js npm or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
The build, bundle and cache commands
In our previous tutorial we looked at the audit, bin and bugs cli commands, in this tutorial we will examine how the npm-build, npm-bundle and npm-cache work.
Npm-build
The npm build is used to build a package, the synopsis is given in the next section.
Synopsis
is a folder that contains a package.json in its root.
Description
This is the plumbing command that is called by npm link and npm install.
Generally, this command is called during installation, however, if you need to run it directly, you can run:
Npm-bundle
The npm-bundle command has been removed. Npm removed the command as from v1.0, the reason behind the removal is that the default behavior now is to install packages into the local space.
Hence, you should just use the npm install to do what the npm bundle used to do.
Npm-cache
The npm cache is used to manipulate the cache of packages.
Synopsis
Description
The npm-cache cli command is used to add, list, or clean the npm cache folder.
DETAILS
npm will store cache data in an opaque directory within the configured cache, that is named _cacache. This directory is a cacache-based content-addressable cache that will store all http request data as well as other package-related data. The primary way to access this directory is through the pacote, which is the library responsible for all package fetching as of [email protected]
Currently there is no method exposed through npm to inspect or directly manage the contents of this cache. If you have to access it, you have to used cacache directly.
npm does not remove data by itself: the cache grows as new packages are installed.
A NOTE ON THE CACHE’S DESIGN
The npm cache is a cache and nothing more: You should not rely on it as a persistent and reliable data store for package data. npm cannot guarantee that a previously-cached piece of data will be available later, also, npm automatically deletes corrupted contents. The primary guarantee that the cache makes is that, when it does return data, that data will be exactly the data that was inserted into it.
If you want to run an offline verification of existing cache contents, you should use npm cache verify.
Configuration
cache
This is the root cache folder.
Default:
/.npm on Posix, or %AppData%/npm-cache on Windows.
Share this Tutorial / Exercise on : Facebook and Twitter