Development

How to Clear NPM Cache: Linux, Windows, Linux

This tutorial is going to walk you through the commands to use to clear the npm cache.
Captain Salem 2 min read
How to Clear NPM Cache: Linux, Windows, Linux

We are all familar with NPM.

Node Package Manager, commonly known as NPM is a package manager to the JavaScript ecosystems such as Node.js. NPM comes with a command-line client and an online database for public and private packages we call the npm registry.

There are various instances where you might need to clear the npm cache within your given project.

What is NPM Cache?

Before we proceed, what exactly is npm cache?

NPM cache is a local storage mechanims used by the npm package manager to store copies of the packages you downlaod from the NPM registry.

The cache helps to speed up the installation of packages by reducing the need to fetch packages from the network if they already exists in the package

How it Works?

When we install a package using npm, it first checks the cache to see if the package is present and the correct version is available.

If so, npm will use the cached version instead of sending a network request. The cache can significantly improve installation times and reduce bandwidth usage, especially for frequently used packages or in environments with slow network connections.

Why Clear the NPM Cache?

A common reason to wipe out the npm cache is to remove any corrupted files. This can occur when npm fails to download various packages or fetching the incorrect versions, etc.

You can also clear the npm cache to just reclaim disk space.

📌
Manual cache clearing is less common as the cache is designed to be self-maintaining and to automatically recover in case of corruption.

Where is the Cache Stored?

On Unix-like operating systems, the cache folder is located in the ~/.npm directory while on Windows, in the %LocalAppData%\npm-cache directory.

NPM Clear Cache

To delete the cached data from the npm ecosystem, use the command as shown:

npm cache clean

This should perform a manual clean of the npm cache.

You can also force delete all the data from the cache folder using the --force option as shown:

npm cache clean --force

Once cleared, you can use the verify command to check that the cache has been successfully cleared as:

npm cache verify

Clear NPM Cache: React and React Native

If you are working with a React or React Native project, you can clear the cache by using the --reset-cache option as shown:

npm start -- --reset-cache

The -- option forwards the --cache-reset option to the command executed by the npm start script.

Conclusion

In this tutorial, we learned about the npm cache, what it is, how it works, why we may need to manually clear it, and the commands to use to manually clean the npm cache.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.