Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Clear the npm Cache

This guide shows you how to clear npm's cache and check that it is healthy. It helps when installs fail with strange errors.

How-To Guides2 min read8 steps3 screenshots

What is npm and its cache?

npm is a tool that downloads JavaScript packages for Node.js projects. A package is a bundle of ready-made code you can reuse. When npm downloads a package, it keeps a copy on your computer. That saved copy is the cache. Next time, npm can reuse it and skip the download. Sometimes a copy gets broken. Then installs fail or act oddly. Clearing the cache gives npm a clean start.

Steps

  1. Open a terminal. A terminal is a window where you type commands. On a hosting server, log in with SSH first.
  2. Find where the cache is stored.
    npm config get cache
    This prints a folder path. On Linux it is often ~/.npm.
  3. Check that the cache is healthy.
    npm cache verify
    This looks for bad files and removes what is not needed. Often this is enough.
  4. If problems stay, clear the whole cache.
    npm cache clean --force
    npm asks you to use --force as a safety step, because a good cache saves time.
    execute on the terminal
  5. Run your install again.
    npm install

If it still fails

Delete the project's installed packages and its lock file, then install again. Take a copy of package-lock.json first.

  1. Go to your project folder.
  2. Remove the packages folder.
    rm -rf node_modules
    This deletes the node_modules folder. It is safe, because npm install builds it again.
  3. Run npm install.

Check your versions

Old Node.js or npm versions cause many errors. Check them like this.

node -v
npm -v

These print the two version numbers. If yours are very old, ask Hostvento support whether you can upgrade on your plan.

Tip: The older command npm cache clean without --force no longer works in modern npm. Use the forced form.

Quick recap

  • The npm cache stores downloaded packages.
  • Run npm cache verify first.
    npm cache verify
    npm start script
  • Use npm cache clean --force to wipe it.
  • Then run npm install again.