Language: EN


How to manage the NPM cache

The NPM cache is a local directory where previously downloaded packages and dependencies are stored.

When we install or update a package, NPM looks in its local cache before downloading it from the package registry.

If the package or dependency is already in the cache, it uses that copy instead of downloading it again, saving us the wait.

It is normal that we don’t have to touch the NPM cache frequently, or almost never. But if you ever have a problem, here’s how to fix it.

Cache Configuration

The NPM cache configuration can be customized according to our needs. To access the NPM configuration, we can use the following command:

NPM config edit

This will open the NPM configuration file in our default text editor.

Here we can modify different parameters related to the cache, such as the cache storage directory (cache) and the maximum cache duration (cache-min).

For example, if we want to change the cache storage directory to a custom directory called “my-cache”, we can add the following line to the configuration file:


That said, unless we are very clear about what we are doing, or we have a very rare error that forces us to change it, we normally don’t have to touch this file.

Finally, the NPM cache files are stored in these folders

  • %LocalAppData%\npm-cache in Windows
  • ~/.npm in Linux

But, even more so, it is normal that we never have to manually touch the files in these folders.

Cache Cleanup

As we work with NPM, the cache can accumulate outdated or unnecessary packages and dependencies. To free up disk space, it is not a bad idea to do a periodic cleanup.

We can clean the NPM cache using the following command:

npm cache clean

This will remove all packages and dependencies that are not in use, freeing up disk space.

Also, in some cases, it is not common but it can be that the cache gives us problems with some library. Because it has gotten stuck, or who knows… in that case, cleaning the cache is usually one of the first measures.

If we use the --force command to force the removal of all items from the cache, regardless of whether they are in use or not.

npm cache clean --force

The cache will be clean, as if NPM were just installed. Nothing will corrupt in any project, or be lost. Simply, the next time you install a package, it may take a little longer, because it will have to download everything.

Cache Verification

NPM cache verification allows us to check if a specific package or dependency is in the cache before downloading it. To verify if a package is in the cache, we can use the following command:

npm cache verify <package-name>

If the package is in the cache, NPM will display a message confirming its presence. Otherwise, NPM will download the package from the registry.