Talk to the NOC, IST hours — +91 75994 50220 support@bigdomainhost.com
Guide

How to clear the npm cache, and when you should not bother

Clearing the npm cache is the first thing many developers try when an install fails. npm's own documentation says it is rarely the fix. Here is the command, what it really does, and what to run instead.

Short answer: Run npm cache clean --force to delete the npm cache. The --force flag is required because npm's cache verifies itself and normally never needs clearing, so try npm cache verify first. The cache is at ~/.npm on macOS and Linux and %LocalAppData%\npm-cache on Windows. For most install errors, delete node_modules and run npm ci instead.

The short answer: npm cache clean --force

To clear the npm cache, run npm cache clean --force. The --force flag is required, because npm treats its cache as self-healing and does not expect you to clear it. In most cases the safer first step is npm cache verify, which checks the cache and removes data that is no longer needed without throwing everything away.

# check the cache and garbage-collect it (safe)
npm cache verify

# delete the whole cache (needs --force)
npm cache clean --force

Both commands work the same way on Windows, macOS and Linux. Neither one touches your project's node_modules folder or package-lock.json, which is why clearing the cache so often fails to fix an install problem. More on that below.

Why npm asks for --force

The npm documentation is direct about it: clearing the cache is "typically unnecessary", because the cache is self-healing and resistant to data corruption. All data that passes through the cache is verified for integrity when it is written and again when it is read. If an entry turns out to be corrupted, npm either raises an error or fetches the data again automatically.

For that reason the docs say it should never be necessary to clear the cache for any reason other than reclaiming disk space, and that is why clean requires --force. If you run npm cache clean on its own, npm stops with an error instead of deleting anything. The flag is npm's way of asking whether you are sure.

The docs also describe the cache as strictly a cache. npm makes no promise that something cached earlier will still be there later. The one guarantee is that, if the cache does return data, it is exactly the data that was put in.

npm cache verify: the safer first step

npm cache verify verifies the contents of the cache folder, garbage-collects any unneeded data, and checks the integrity of the cache index and all cached data. It works offline and deletes only what is broken or unneeded, so there is no harm in running it whenever an install behaves strangely.

If verify finishes without complaint and the install still fails, the cache is very unlikely to be your problem. Move on to the reinstall steps below.

Where the npm cache lives

SystemDefault cache folder
Windows%LocalAppData%\npm-cache
macOS~/.npm
Linux~/.npm

Your machine may be configured differently, so ask npm:

npm config get cache

Inside that folder, the package data sits in a directory named _cacache. It is a content-addressable store that npm manages, so do not edit files in it by hand. npm does not remove cache data by itself, and the folder grows as you install new packages. On macOS or Linux you can see how large it is with du -sh ~/.npm.

When clearing the cache helps, and when it does not

SymptomWill clearing the cache help?What to do
Disk is full, or the cache folder has grown to several GBYes. This is the one reason npm's docs give.npm cache verify, then npm cache clean --force if you need the space
EINTEGRITY or a checksum mismatch during installSometimesRun npm cache verify and retry. If it persists, the hash in the lockfile does not match what your registry or proxy serves, so check npm config get registry
Corrupted tarball, or invalid JSON from the registrySometimesVerify, retry, then clean. Also check any corporate proxy or mirror in between
"Cannot find module" after an install that looked fineNoDelete node_modules and reinstall
npm ci says the lockfile and package.json do not matchNoRun npm install once and commit the updated lockfile
Works on one laptop, fails on anotherRarelyCompare node -v and npm -v, then reinstall from the lockfile with npm ci
Dependency version conflictsNoFix the version ranges in package.json

The pattern is simple. The cache holds downloaded packages. Your project's state lives in node_modules and the lockfile. Most "npm is broken" moments come from the second place, not the first.

A clean reinstall sequence

Work down this list and stop as soon as the install succeeds.

  1. Verify the cache. npm cache verify
  2. Reinstall exactly what the lockfile says. npm ci removes node_modules for you and installs from package-lock.json.
  3. If npm ci reports a mismatch, run npm install so npm updates the lockfile, review the change, and commit it.
  4. Delete node_modules by hand and install again. Useful when a file is locked or half-written.
  5. Last resort: delete the lockfile too. This makes npm resolve every version again, so you may get newer releases of your dependencies. Run your tests before committing the new lockfile.
  6. Only now, clear the cache with npm cache clean --force and install once more.

On macOS, Linux or Git Bash:

rm -rf node_modules
npm install

On Windows PowerShell:

Remove-Item -Recurse -Force node_modules
npm install

Run these from the project folder, the one that contains package.json. Check the path before you press Enter, because a recursive delete does not ask twice.

npm ci: the clean install built for automation

npm ci is similar to npm install, but it is meant for test platforms, continuous integration and deployments. The documentation lists the differences:

  • The project must already have a package-lock.json or npm-shrinkwrap.json.
  • If the lockfile and package.json disagree, npm ci exits with an error instead of updating the lockfile.
  • It installs whole projects only. You cannot add a single dependency with it.
  • If node_modules exists, it is removed before the install starts.
  • It never writes to package.json or the lockfile.

That makes it the right command for build servers and for the "start from a clean state" step on your own machine.

Yarn and pnpm equivalents

ToolClear or tidyFind the folder
npmnpm cache verify, npm cache clean --forcenpm config get cache
Yarn 1 (Classic)yarn cache cleanyarn cache dir
Yarn 2 and lateryarn cache clean, with --mirror for the global cache or --all for bothShown in the project's Yarn configuration
pnpmpnpm store prunepnpm store path

In Yarn Classic, yarn cache clean clears the global cache, and you can pass package names to remove only those. In modern Yarn the same command removes the local cache files unless you add --mirror or --all.

pnpm keeps packages in a store instead of a plain cache. pnpm store prune removes packages that no project on the machine references any more. pnpm's docs call it not harmful, because anything removed is downloaded again when needed. Our npm vs pnpm comparison explains how the store works.

Clearing the npx cache

When you run a package with npx that is not installed in the current project, npm installs it into a folder inside the npm cache and runs it from there. Recent npm releases (the commands below are documented for npm 11) can list and remove those entries:

npm cache npx ls
npm cache npx info <key>
npm cache npx rm <key>

If your goal is only to run a newer release of a tool, ask for it by version or tag, for example npx some-tool@latest, instead of clearing anything.

CI and Docker notes

  • Use npm ci in pipelines. It fails loudly when the lockfile is out of date, which is what you want in a build.
  • Cache the npm cache folder between runs, not node_modules. The npm docs show a CI example that keeps $HOME/.npm. npm ci deletes node_modules anyway.
  • Do not add npm cache clean --force to every build. It forces a full download each time and fixes nothing.
  • In a Dockerfile, copy the manifests first. Copy package.json and package-lock.json, run npm ci, then copy the rest of the source. Docker can then reuse the dependency layer until the lockfile changes.
  • Clearing the cache in an image is a fair use. The cache written during npm ci stays in that image layer. Cleaning it in the same RUN step keeps the image smaller, which is the disk-space case npm's docs allow for.
FROM node:24-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY . .
CMD ["node", "server.js"]

Here --omit=dev leaves out devDependencies, which suits a production image. Change the Node.js tag and the start command to match your project. On a Bigdomainhost KVM VPS, Docker runs natively, so you can build and run this image on the server itself.

Running Node.js projects on Bigdomainhost

Bigdomainhost's shared cPanel plans are built for PHP sites and WordPress, so a Node.js app belongs on a KVM VPS: full root access, NVMe storage, an Ubuntu 24.04 image and a choice of Mumbai or Delhi NCR. The current price is on the pricing page. If you are starting from a fresh server, our Ubuntu VPS setup guide covers the first steps. Support is on WhatsApp at +91 75994 50220 at any time, by phone in IST business hours, and at support@bigdomainhost.com.

Sources (checked )

How Bigdomainhost can help

If your Node.js app has outgrown your laptop or a shared plan, run it on a Bigdomainhost KVM VPS: full root access, NVMe storage that makes npm ci quick, Docker if you want it, and RAM reserved for you. Servers in Mumbai or Delhi NCR are usually ready in under 60 seconds, billed in 30-day periods from a rupee wallet with no setup fee. If a build or deploy goes wrong, WhatsApp our engineers at any time.

Deploy your Node.js app Ask an engineer on WhatsApp

Answers

Frequently asked questions

Still unsure? Call +91 75994 50220 or write to support@bigdomainhost.com — a human replies in IST hours.

How do I clear the npm cache?

Run npm cache clean --force in a terminal. The command is the same on Windows, macOS and Linux, and it deletes the downloaded package data in npm's cache folder. It does not change node_modules or package-lock.json in your project.

Why does npm cache clean need --force?

npm's documentation says the cache is self-healing and checks the integrity of everything it stores, so clearing it is typically unnecessary. Without --force npm refuses to delete the cache. The only reason the docs give for clearing it is to reclaim disk space.

What does npm cache verify do?

It verifies the contents of the cache folder, garbage-collects data that is no longer needed, and checks the integrity of the cache index and the cached data. It is safe to run at any time and works offline. Try it before a full clean.

Where is the npm cache located?

By default it is in ~/.npm on macOS and Linux and in %LocalAppData%\npm-cache on Windows. Run npm config get cache to see the folder on your machine. The package data is stored in a subfolder named _cacache.

Is it safe to delete the npm cache?

Yes. The cache only holds copies of packages that can be downloaded again, so nothing in your projects is lost. The cost is that the next installs are slower and need a network connection, because everything has to be fetched again.

npm cache clean did not fix my error. What next?

Most install problems live in node_modules or the lockfile, not the cache. Run npm ci to reinstall exactly what package-lock.json lists, or delete node_modules and run npm install. If npm ci says the lockfile does not match package.json, run npm install once and commit the updated lockfile.

Support that picks up the phone.

In IST hours, in Hindi or English, and WhatsApp replies fastest at any time. Sales, migration and emergencies all reach the same engineers. No offshore queue, no 48-hour first reply.

Need cPanel or WHM with the ports already sorted?

Web & cPanel hosting comes with cPanel ready to log in to, reseller hosting adds white-label WHM, and a KVM VPS can have cPanel & WHM pre-installed. Call +91 75994 50220 or email support@bigdomainhost.com.