AjapaReporter

Home life · Updated

How to Clean npm Cache: Step-by-Step Guide

Time it takes5 min
Steps5in the list below
LevelEasyno special skills
Official sourcenpm Docscheck it for changes

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 -v and npm -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

  1. Check the cache location – Run npm config get cache to see where npm stores its cache. The default is ~/.npm on Unix-like systems and %LocalAppData%\npm-cache on Windows.
  2. 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.
  3. 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.
  4. Confirm the cache is cleared – Run npm cache verify again. It should show that the cache is empty or significantly smaller. You can also check the folder size manually if you want.
  5. Rebuild your project dependencies – In your project folder, run npm install to 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 --force when a simple npm cache verify would fix the issue without wiping everything.
  • Using the wrong command syntax – Older npm versions used npm cache clean without --force. In newer versions, you must include --force or 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 install again to get fresh packages.
  • Ignoring permission errors – If you get EACCES errors, you might need to run the command with sudo on macOS/Linux or as Administrator on Windows, but avoid using sudo unless necessary.

Tips

  • Use verify as a routine check – Run npm cache verify periodically 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 ls to list cached packages or du -sh ~/.npm on 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 pnpm or yarn, which have their own cache management.

Troubleshooting

  • Cache clean does nothing – If npm cache clean --force doesn’t reduce the cache size, you may be looking at the wrong folder. Check the path from npm config get cache and 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 install still throws errors, the problem might be in your package.json or network. Try deleting node_modules and package-lock.json, then run npm install again.
  • 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.

Sources: npm Docs. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.