Skip to content

How to Fix npm Cache Issues (npm cache clean --force)

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix npm Cache Issues (npm cache clean. We cover key concepts, practical examples, and best practices.

The Problem

npm install fails with integrity check failed, sha1 mismatch, or Unexpected token errors, indicating a corrupted package cache. Stale or corrupted cached tarballs cause npm to install broken packages.

Quick Fix

Step 1: Verify the cache integrity

Check if the cache has corrupted entries without deleting it yet:

npm cache verify
Cache verified, all checks OK

Or if corrupted:

Cache verified, content garbage collected: 12 entries (42.5 MB)

Step 2: Clean the entire cache

Force clear the cache and reinstall:

npm cache clean --force
npm WARN using --force Recommended protections disabled.

Step 3: Delete node_modules and reinstall

After clearing the cache, also remove node_modules for a fresh start:

rm -rf node_modules package-lock.json
npm install

This rebuilds node_modules from scratch with freshly downloaded packages.

Step 4: Check npm cache location and size

Find where npm stores cached packages:

npm cache ls 2>/dev/null | head -5

View the cache directory location and its size:

npm config get cache
du -sh $(npm config get cache)
/home/user/.npm
245M    /home/user/.npm

Step 5: Prevent cache corruption with a lockfile

Commit package-lock.json to ensure consistent installs across environments:

git add package-lock.json
git commit -m "Add package-lock.json"

Alternative Solutions

Use npm ci for cleaner installs

The npm ci command deletes node_modules and uses the lockfile directly:

npm ci

Check for disk space issues

A full disk can cause npm install to appear corrupted. Check with df:

df -h

Common Mistakes to Avoid

Running npm cache clean too frequently. The npm cache speeds up installs significantly. Only clean it when you suspect corruption.

Not deleting node_modules after cache clean. A corrupted cache means the existing node_modules may also be corrupted. Delete both.

Using npm cache clean in CI unnecessarily. This slows down CI builds. Use npm ci instead, which is faster and skip cache manipulation.

Pro Tips

Use npm ci for deterministic builds. The npm ci command installs dependencies exactly from package-lock.json and deletes node_modules first.

Measure cache size periodically. Track the npm cache size with du -sh $(npm config get cache) to know when to clean it.

Use npm cache ls to inspect cached packages. Before cleaning, inspect what is cached to determine if a targeted clean is possible.

Use npm ci in deployment scripts. Unlike npm install, npm ci installs directly from package-lock.json without modifying it, and it fails if the lock file is out of sync with package.json.

Prevention

  • Run npm cache verify periodically to catch corruption early.
  • Commit package-lock.json to version control.
  • Use npm ci instead of npm install in CI environments for deterministic installs.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro