How to set maximum size limit for NPM cache storage

My disk space is getting filled up and I found out that npm cache is using tons of storage on my system. I’m wondering if there’s a way to put a cap on how much space npm can use for caching. I’ve been searching around but can’t find any built-in setting to limit the cache size. Has anyone figured out how to do this or do I just need to manually clean it regularly?

Unfortunately, npm does not provide a built-in feature to limit the cache size. I faced a similar issue last year when my npm cache grew unexpectedly large. To manage it, I created a monthly cron job that executes npm cache clean --force to prevent the cache from consuming excessive disk space. Before performing the clean, running npm cache verify can help identify any corrupted packages. Additionally, I’ve found success by relocating the cache to a dedicated partition using npm config set cache /path/to/cache, ensuring it doesn’t occupy space on my primary drive. While the cache does enhance installation speed, routine maintenance appears to be the most viable solution.

yeah, npm cache grows huge fast. I check mine with npm cache ls and run npm cache clean when it gets bloated. you can also move it somewhere with more space using npm config set cache /new/path. works well enough, though i really wish they’d just add a proper size limit already.

NPM doesn’t have a built-in size limit, but here’s what works for me. I use npm config set cache-max 86400000 to set cache expiration - stops it from growing forever. I also wrote a quick script that checks cache size weekly with du -sh ~/.npm and auto-cleans when it hits my limit. You can try npm config set prefer-offline false to cut down on aggressive caching too. I’ve found 2-3GB of cache hits the sweet spot for performance without eating too much space. Running npm cache verify regularly helps catch bloated entries before they get out of hand.