How to Clear the npm Cache on Linux, macOS, and Windows | Warp
Introducing Warp Factories: open, flexible infrastructure for building your own cloud software factory. Learn More
The short answer
To delete all data out of the cache folder on Linux, macOS and Windows, you can use the following npm cache command:
Bash
$ npm cache clean --force
To then verify that the cache has been successfully cleared, you can run the following command:
Bash
$ npm cache verify
Easily retrieve this command using Warp’s AI Command Search
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering clear npm cache in the AI Command Search will prompt an npm command that can then quickly be inserted into your shell by doing CMD+ENTER.
The npm cache system
When installing a package for the first time, npm will download the package in the node_modules 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 ~/.npm directory, and on Windows, in the %LocalAppData%\npm-cache directory.
Note that there is, at the moment, no available npm command to easily verify the content of the cache folder.
Why clear the npm 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.
Clearing the npm cache in React or React Native projects
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:
Bash
$ npm start -- --reset-cache
Where the -- argument is used to forward the --reset-cache option to the command executed by the npm start script.
If this command doesn't work, you can use the following commands to:
- Clear the list of files and directories watched by the watchman daemon.
Bash
$ watchman watch-del-all
- Remove the cache directories created by React Native and Metro.
Bash
$ rm -rf $TMPDIR/react-native-packager-cache-*
$ rm -rf $TMPDIR/metro-bundler-cache-*
- Remove the node_modules directory, clear the npm cache, and reinstall the npm packages.
Bash
$ rm -rf node_modules
$ npm cache clean --force
$ npm install