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
| System | Default 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
| Symptom | Will clearing the cache help? | What to do |
|---|---|---|
| Disk is full, or the cache folder has grown to several GB | Yes. 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 install | Sometimes | Run 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 registry | Sometimes | Verify, retry, then clean. Also check any corporate proxy or mirror in between |
| "Cannot find module" after an install that looked fine | No | Delete node_modules and reinstall |
npm ci says the lockfile and package.json do not match | No | Run npm install once and commit the updated lockfile |
| Works on one laptop, fails on another | Rarely | Compare node -v and npm -v, then reinstall from the lockfile with npm ci |
| Dependency version conflicts | No | Fix 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.
- Verify the cache.
npm cache verify - Reinstall exactly what the lockfile says.
npm ciremovesnode_modulesfor you and installs frompackage-lock.json. - If
npm cireports a mismatch, runnpm installso npm updates the lockfile, review the change, and commit it. - Delete
node_modulesby hand and install again. Useful when a file is locked or half-written. - 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.
- Only now, clear the cache with
npm cache clean --forceand 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.jsonornpm-shrinkwrap.json. - If the lockfile and
package.jsondisagree,npm ciexits with an error instead of updating the lockfile. - It installs whole projects only. You cannot add a single dependency with it.
- If
node_modulesexists, it is removed before the install starts. - It never writes to
package.jsonor 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
| Tool | Clear or tidy | Find the folder |
|---|---|---|
| npm | npm cache verify, npm cache clean --force | npm config get cache |
| Yarn 1 (Classic) | yarn cache clean | yarn cache dir |
| Yarn 2 and later | yarn cache clean, with --mirror for the global cache or --all for both | Shown in the project's Yarn configuration |
| pnpm | pnpm store prune | pnpm 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 ciin 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 cideletesnode_modulesanyway. - Do not add
npm cache clean --forceto every build. It forces a full download each time and fixes nothing. - In a Dockerfile, copy the manifests first. Copy
package.jsonandpackage-lock.json, runnpm 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 cistays in that image layer. Cleaning it in the sameRUNstep 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 )
- npm Docs: npm cache (CLI v11)
- npm Docs: npm ci (CLI v11)
- npm Docs: npm exec and npx (CLI v11)
- Yarn: yarn cache clean
- Yarn Classic: yarn cache
- pnpm: pnpm store
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.