How can I change the cache path for npm (or completely disable the cache) on Windows?

node.jsNpm

node.js Problem Overview


I've installed Node.js on my Windows 7 x64 development machine, the manual way:

mkdir C:\Devel\nodejs
cd C:\Devel\nodejs
set NODE_PATH=%CD%
setx /M PATH "%PATH%;%NODE_PATH%"
setx /M NODE_PATH "%NODE_PATH%\node_modules"

I've placed the main node x64 binary along with npm package manager in C:\Devel\nodejs. Works like a charm and I can update the main binary without dealing with the installer.

The only problem I can't solve is moving the cache folder. When I install a local package:

npm install express

... cache is placed under %APP_DATA%\npm-cache folder. I'd like to change it to:

C:\Devel\nodejs\npm-cache

How can I change the npm cache folder, or disable it completely?

node.js Solutions


Solution 1 - node.js

You can change npm cache folder using the npm command line. (see https://docs.npmjs.com/cli/v6/using-npm/config#cache)

So you might want to try this command :

> npm config set cache C:\Devel\nodejs\npm-cache --global 

Solution 2 - node.js

You can also set an environment variable with export npm_config_cache=/path/to/cache (Unix) or set npm_config_cache=C:\path\to\cache (Win) as an alternative to npm config set (this is true for all config options in npm).


For anyone using docker you can add the env var at runtime with:

docker run -e npm_config_cache=/path/to/cache mydockerimage:tag

Solution 3 - node.js

You can also do following:

For having cache path as you wish, for a single package while installing it:

npm install packageName --cache path/to/some/folder

For having cache path as you wish, for all the packages in package.json:

Just be in the directory where package.json is as usual and do

npm install --cache path/to/some/folder

You may not find this in npm documentation but i have tried it with npm 6 and it works. Looks like it works since npm 5 [Refer: https://stackoverflow.com/questions/44310188/how-to-specify-cache-folder-in-npm5-on-install-command]

Solution 4 - node.js

In Windows you can simply cd to the desired cache folder and do npm set cache --global

Solution 5 - node.js

In addition, I found that running an update command works also - for example:

npm update npm

Lastly, one can check their npm-cache directory to see if is being filled or not.

Solution 6 - node.js

Solution

Paste the following code into npmrc file.

Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc

prefix=D:\nodejs\npm
cache=D:\nodejs\npm-cache

Notes: There is no '.' in front of npmrc

Diagrams

NPMRC file folder look like this

enter image description here

NPMRC Content look like this

enter image description here

Hope it helps. Cheers

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongremoView Question on Stackoverflow
Solution 1 - node.jsjcreignouView Answer on Stackoverflow
Solution 2 - node.jsgibView Answer on Stackoverflow
Solution 3 - node.jsLuke P. IssacView Answer on Stackoverflow
Solution 4 - node.jsStanley85View Answer on Stackoverflow
Solution 5 - node.jsDoesEatOatsView Answer on Stackoverflow
Solution 6 - node.jsLAW WEI LIANGView Answer on Stackoverflow