Last updated: Jun 6, 2023
Reading time·3 min
--cache
flag to change the NPM cache locationnpm config edit
command to set your preferred cache locationYou 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.
# 👇️ 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.
npm --global cache verify
You can also run the npm config get cache
command to verify the path of the
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:
cd
command to change the terminal to your preferred cache location.npm set cache --global
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.
npm config delete cache
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.
export npm_config_cache=/path/to/cache
On Windows, use the set command in 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.
npm --global cache verify npm config get cache
--cache
flag to change the NPM cache locationYou can also use the --cache
flag to change the NPM cache location for a
specific npm install
command.
# Windows npm install --cache "C:\path\to\cache" # macOS or Linux npm install --cache /path/to/cache
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.
# 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.
npm config edit
command to set your preferred cache locationYou can also use the following command to set your preferred cache location.
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.
; cache=~/.npm
You can learn more about the related topics by checking out the following tutorials: