Clear npm Cache

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

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

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:

  1. Clear the list of files and directories watched by the watchman daemon.
Bash
$ watchman watch-del-all
  1. Remove the cache directories created by React Native and Metro.
Bash
$ rm -rf $TMPDIR/react-native-packager-cache-*
$ rm -rf $TMPDIR/metro-bundler-cache-*
  1. 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
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

How To Reinstall Packages With Npm

Brief guide to reinstalling npm packages using npm