How to change the NPM Cache location [4 easy Ways]

avatar
Borislav Hadzhiev

Last updated: Jun 6, 2023
3 min

banner

# Table of Contents

  1. How to change the NPM Cache path (location)
  2. Changing the NPM Cache path with an environment variable
  3. Using the --cache flag to change the NPM cache location
  4. Using the npm config edit command to set your preferred cache location

# How to change the NPM Cache path (location)

You can use the npm config set cache command to change the NPM cache location.

The command will update the global cache path in your .npmrc file.

Open your terminal and make sure to update the path to your preferred cache location.

shell
# 👇️ on Windows npm config set cache "D:\bobbyhadz\nodejs\npm-cache" --global # 👇️ on macOS or Linux npm config set cache /home/bobbyhadz/Desktop/nodejs/npm-cache --global

Note that on Windows, you might have to wrap the path to the cache directory in double quotes.

The npm config set cache command has the following format npm config set cache /path/to/cache/dir --global.

After you change the NPM cache path, you can run the npm --global cache verify command to verify that the new cache location has been successfully set.

shell
npm --global cache verify

verify cache location set successfully

You can also run the npm config get cache command to verify the path of the cache.

shell
npm config get cache

npm config get cache

The default path for the NPM cache on Windows is %AppData%/npm-cache or %LocalAppData%\npm-cache.

The default path for the cache on macOS and Linux is ~/.npm which translates to /home/YOUR_USER/.npm.

The path is controlled by the cache configuration parameter.

If the change of the cache location hasn't been applied successfully, try to:

  1. Open your terminal.
  2. Use the cd command to change the terminal to your preferred cache location.
  3. Once your terminal is in the preferred cache directory, issue the following command.
shell
npm set cache --global
  1. Run the following commands to verify that the cache path has been updated.
shell
npm --global cache verify npm config get cache

If at any point you want to unset the cache key in all config files, you can use the npm config delete cache command.

shell
npm config delete cache

# Changing the NPM Cache path with an environment variable

You can also set an environment variable to change the NPM cache path.

On macOS and Linux, open bash or zsh and set the npm_config_cache environment variable.

shell
export npm_config_cache=/path/to/cache

On Windows, use the set command in CMD.

cmd
set npm_config_cache=C:\path\to\cache

Make sure to update the path that points to your new cache directory.

You can run one of the following commands to verify that the cache location has been updated.

shell
npm --global cache verify npm config get cache

# Using the --cache flag to change the NPM cache location

You can also use the --cache flag to change the NPM cache location for a specific npm install command.

shell
# Windows npm install --cache "C:\path\to\cache" # macOS or Linux npm install --cache /path/to/cache

npm install set cache path

Make sure your terminal is located in the root directory of your project (where your package.json file is).

The command above installs all packages from your package.json file and uses the specified cache path.

You can also set the cache path when installing a specific package.

shell
# Windows npm install axios --cache "C:\path\to\cache" # macOS or Linux npm install axios --cache /path/to/cache

install specific npm package with given cache path

Make sure to replace axios with the name of the package you're trying to install with a specified cache path.

# Using the npm config edit command to set your preferred cache location

You can also use the following command to set your preferred cache location.

shell
npm config edit --global

The command should open the global npmrc file in a text editor.

Note that lines that start with semicolons are comments.

Search for the cache key, uncomment it by removing the leading semicolon and update the path.

The cache key should be around line 31.

The default cache key on macOS and Linux will look similar to the following.

shell
; cache=~/.npm

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.