Home life · Updated
How to Clean npm Cache: Step-by-Step Guide
Quick answer Cleaning the npm cache removes corrupted or outdated package data, which can fix install errors and free up disk space. Run npm cache clean --force or use npm cache verify for a safer check.
What You’ll Need
- Node.js and npm installed (check with
node -vandnpm -v) - Terminal or command prompt (Command Prompt on Windows, Terminal on macOS/Linux)
- Administrator or sudo access if you encounter permission errors
Step-by-Step
- Check the cache location – Run
npm config get cacheto see where npm stores its cache. The default is~/.npmon Unix-like systems and%LocalAppData%\npm-cacheon Windows. - Verify the cache integrity – Run
npm cache verify. This scans the cache for errors and removes corrupted entries. It also reports how much space is used. This is the safest first step and often fixes issues without a full clean. - Force clean the cache (if needed) – If verify doesn’t solve your problem, run
npm cache clean --force. This deletes all cached data. npm may warn you that this is not recommended unless you have a specific reason, but it’s safe in most cases. - Confirm the cache is cleared – Run
npm cache verifyagain. It should show that the cache is empty or significantly smaller. You can also check the folder size manually if you want. - Rebuild your project dependencies – In your project folder, run
npm installto re-fetch packages. This ensures your node_modules are fresh and not based on corrupted cache data.
Common Mistakes
- Running clean without verifying first – Many users jump straight to
npm cache clean --forcewhen a simplenpm cache verifywould fix the issue without wiping everything. - Using the wrong command syntax – Older npm versions used
npm cache cleanwithout--force. In newer versions, you must include--forceor npm will prompt you. - Forgetting to reinstall dependencies – Cleaning the cache doesn’t affect your existing node_modules, but if you were having install errors, you need to run
npm installagain to get fresh packages. - Ignoring permission errors – If you get EACCES errors, you might need to run the command with
sudoon macOS/Linux or as Administrator on Windows, but avoid using sudo unless necessary.
Tips
- Use verify as a routine check – Run
npm cache verifyperiodically to keep the cache healthy without nuking it. - Clean only when needed – The cache speeds up installs, so don’t clean it just for fun. Clean if you suspect corruption or need disk space.
- Check the cache size first – Use
npm cache lsto list cached packages ordu -sh ~/.npmon Linux/macOS to see the size. This helps you decide if cleaning is worth it. - Use a package manager alternative – If you keep having cache issues, consider using
pnpmoryarn, which have their own cache management.
Troubleshooting
- Cache clean does nothing – If
npm cache clean --forcedoesn’t reduce the cache size, you may be looking at the wrong folder. Check the path fromnpm config get cacheand delete it manually if needed (but be careful). - Permission errors – On Unix systems, if you get EACCES, try
sudo npm cache clean --force. On Windows, open the command prompt as Administrator. Alternatively, change the npm cache directory to a location you own:npm config set cache /path/to/new/cache. - Install still fails after cleaning – If
npm installstill throws errors, the problem might be in your package.json or network. Try deletingnode_modulesandpackage-lock.json, then runnpm installagain. - npm cache verify hangs – This can happen with a very large cache. Wait a bit, or if it’s stuck, close the terminal and restart. You can also delete the cache folder manually after stopping npm processes.
Frequently Asked Questions
Is it safe to delete the npm cache?
Yes, it’s safe. The cache is just a store of downloaded package tarballs. npm will re-download them as needed. You won’t lose any project files or configuration.
Why does npm cache clean require –force?
npm added this requirement to discourage unnecessary cleaning. The cache is meant to speed up installs, and a full clean is rarely needed. The --force flag confirms you understand that.
How often should I clean the npm cache?
Only when you have a specific problem like corrupted packages or install errors, or if you’re low on disk space. There’s no routine schedule; regular use of npm cache verify is enough.
Will cleaning the cache affect my installed packages?
No. The cache is separate from your project’s node_modules. Cleaning it won’t uninstall or modify any existing packages. You just need to re-download if you install again.
Can I move the npm cache to another drive?
Yes. Run npm config set cache D:\npm-cache (Windows) or npm config set cache /mnt/extra/npm-cache (Linux/macOS). This can help if your main drive is full.
What’s the difference between npm cache clean and npm cache verify?
npm cache clean --force deletes the entire cache. npm cache verify checks for corruption, removes bad entries, and reports errors without wiping everything. Use verify first, clean only if needed.
Related
- How to Reply to a Thank You Message Naturally
- How to Print Gridlines in LibreOffice Calc
- How to Change iPhone to 2.4GHz Wi-Fi
Sources: npm Docs. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.