Weblutions Documentation
Weblutions Main Site Contact Us Our Discord
Knowledgebase IconKnowledgebase

Weblutions Documentation / Knowledgebase / How to Clear npm Cache & Reinstall Node Modules

Updated

How to Clear npm Cache & Reinstall Node Modules

By Emily Drayven 1 mins 90

Problems with Node.js dependencies can sometimes cause npm install to hang, fail, or behave unexpectedly. In these cases, clearing the npm cache and reinstalling the project's Node modules can often resolve problems caused by corrupted packages, incomplete installations, or dependency issues.

This process removes the existing installed packages and allows npm to reinstall the dependencies from a clean state.

59f280d9-b1df-4d30-be8d-35daca2d9e26.png

Table of Contents


Basic Clean Install

From the root directory of the Node.js project, remove the existing node_modules directory and reinstall the dependencies:

rm -rf node_modules && npm install

This is generally the first option to try, as the existing package-lock.json remains in place and npm can continue using the previously resolved dependency versions.


Clearing the npm Cache

If installation problems continue, the npm cache can also be cleared before reinstalling the dependencies:

npm cache clean --force && rm -rf node_modules && npm install

The --force option is required when completely clearing the npm cache.

This may help resolve issues caused by cached package data or an incomplete previous installation.


Full Dependency Reset

If problems still remain, a more complete reset can be performed by removing both node_modules and package-lock.json:

npm cache clean --force && rm -rf node_modules package-lock.json && npm install

This command will:

  • Clear the npm cache

  • Remove the existing node_modules directory

  • Remove the current package-lock.json

  • Reinstall all dependencies

  • Generate a new package-lock.json

Removing package-lock.json should generally be reserved for cases where the earlier steps have not resolved the problem, as regenerating the lock file may result in different dependency versions being installed.


If Installation Still Fails

If a clean installation does not resolve the issue, further troubleshooting may be required.

Useful checks include:

node -v
npm -v

Additional installation output can also be displayed using:

npm install --verbose

Other possible causes include network connectivity, npm registry access, file permissions, disk space, Node.js version compatibility, or problems with an individual dependency.