154

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?

gremo
  • 47,186
  • 75
  • 257
  • 421

6 Answers6

233

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 

Then, run npm --global cache verify after running this command.

theMayer
  • 15,456
  • 7
  • 58
  • 90
jcreignou
  • 3,670
  • 1
  • 23
  • 21
  • 3
    Also note that if you're going to go down the npmrc route, the global npmrc file isn't located directly at $PREFIX, but rather in $PREFIX\etc – Henry C Dec 22 '14 at 12:45
  • 3
    When doing this on Windows 7 using Cygwin I had to add a forward slash in front of every backslash to avoid the backslashes being removed. – T. Junghans Mar 27 '15 at 09:34
  • 16
    After executing do a `npm config list` to verify correct setting – kampsj Nov 18 '15 at 16:18
  • I'm having trouble finding command that you are referring, anywhere in linked document – AaA Jul 24 '19 at 09:02
  • 2
    Run `npm --global cache verify` after running this command – smac89 Dec 04 '20 at 06:51
  • I had to use the "environment variable" suggested in another answer because I didn't have `sudo` in the 3rd part image I was building on, and running `npm config set cache` will fail if the root owned files are there. – mhvelplund Jun 25 '23 at 07:45
66

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
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
gib
  • 1,951
  • 1
  • 21
  • 24
  • Just for the sake of curiosity, why would i change the cache directory using dockers? Which scenario are you aware of that could use benefit of this? – fudo Dec 21 '21 at 08:09
  • 3
    @fudo it is often useful to use a persistent cache as a volume mount with docker. If I'm running an `npm install` repeatedly in docker, I can mount a cache directory in from my host machine, so I don't lose the cache every time I run. – gib Jan 19 '22 at 17:31
  • This specific approach is also useful if you want to run npm command as a different user. If the user doesn't exist the path to cache directory will be empty. You can verify this by running: `docker run --rm -u 1500:1500 node npm config get cache`. In vanilla node images there is no user with ID 1500. – pauk960 Oct 11 '22 at 09:43
  • [Here](https://docs.npmjs.com/cli/v9/using-npm/config#environment-variables) is the official documentation of this feature. – Harold L. Brown Mar 17 '23 at 06:52
14

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: How to specify cache folder in npm5 on install command?

Luke P. Issac
  • 1,471
  • 15
  • 32
5

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

Stanley85
  • 389
  • 1
  • 5
  • 7
2

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

LAW WEI LIANG
  • 517
  • 5
  • 11
-3

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.

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
DoesEatOats
  • 625
  • 7
  • 13