Terminus
Clear npm Cache

Clear npm Cache

The short answer

To delete all data out of the cache folder on Linux, macOS and Windows, you can use the following [.inline-code]npm cache[.inline-code] command:

 $ npm cache clean --force

To then verify that the cache has been successfully cleared, you can run the following command:

 $ npm cache verify

[#easily-recall-with-ai]Easily retrieve this command using Warp’s AI Command Search[#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering [.inline-code]clear npm cache[.inline-code] in the AI Command Search will prompt an [.inline-code]npm[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#the-npm-cache-system]The npm cache system[#the-npm-cache-system]

When installing a package for the first time, npm will download the package in the [.inline-code]node_modules[.inline-code] folder of the project and automatically add a local copy of this package into the cache folder.

In the future, when reinstalling the same package, npm will reuse this local copy to speed up the installation process instead of downloading it again from the registry.

On Unix-like operating systems, the cache folder is located in the [.inline-code]~/.npm[.inline-code] directory, and on Windows, in the [.inline-code]%LocalAppData%\npm-cache[.inline-code] directory.

Note that there is, at the moment, no available [.inline-code]npm[.inline-code] command to easily verify the content of the cache folder.

[#why-clear-the-cache]Why clear the npm cache[#why-clear-the-cache]

As of npm@5, all data that passes through the cache is fully verified and automatically refetched in case of corruption. For this reason, it should never be necessary to clear the cache for any reason other than reclaiming disk space or reinstalling libraries free of cache.

[#clear-react-native-cache]Clearing the npm cache in React or React Native projects[#clear-react-native-cache]

The React framework offers a seamless development experience by using multiple caching mechanisms in order to minimize the recompiling and loading time of applications.

However, it sometimes happens that one of these caches doesn't work as intended and doesn't pick up the latest changes made to the application's code or to the list of installed packages it relies on.

To fix this problem, you can restart your React Native application with a clean cache using the following command:

 $ npm start -- --reset-cache

Where the [.inline-code]--[.inline-code] argument is used to forward the [.inline-code]--reset-cache[.inline-code] option to the command executed by the [.inline-code]npm start[.inline-code] script.

If this command doesn't work, you can use the following commands to:

1. Clear the list of files and directories watched by the [.inline-code]watchman[.inline-code] daemon.

 $ watchman watch-del-all

2. Remove the cache directories created by React Native and Metro.

 $ rm -rf $TMPDIR/react-native-packager-cache-*
$ rm -rf $TMPDIR/metro-bundler-cache-*

3. Remove the [.inline-code]node_modules[.inline-code] directory, clear the npm cache, and reinstall the npm packages.

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