node.js 如何在 Windows 上更改 npm 的缓存路径(或完全禁用缓存)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14836053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the cache path for npm (or completely disable the cache) on Windows?
提问by gremo
I've installed Node.js on my Windows 7 x64 development machine, the manual way:
我已经在我的 Windows 7 x64 开发机器上安装了 Node.js,手动方式:
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 binaryalong with npm package managerin C:\Devel\nodejs. Works like a charm and I can update the main binary without dealing with the installer.
我已经将主节点 x64 二进制文件和npm 包管理器放在C:\Devel\nodejs. 像魅力一样工作,我可以在不处理安装程序的情况下更新主二进制文件。
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-cachefolder. I'd like to change it to:
...缓存放在%APP_DATA%\npm-cache文件夹下。我想把它改成:
C:\Devel\nodejs\npm-cache
C:\Devel\nodejs\npm-cache
How can I change the npm cache folder, or disable it completely?
如何更改 npm 缓存文件夹,或完全禁用它?
回答by jcreignou
You can change npm cache folder using the npmcommand line. (see : https://docs.npmjs.com/misc/configand more specifically https://docs.npmjs.com/misc/config#cache)
您可以使用npm命令行更改 npm 缓存文件夹。(参见:https: //docs.npmjs.com/misc/config,更具体地说是https://docs.npmjs.com/misc/config#cache)
So you might want to try this command :
所以你可能想试试这个命令:
> npm config set cache C:\Devel\nodejs\npm-cache --global
回答by gib
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).
您还可以使用export npm_config_cache=/path/to/cache(Unix) 或set npm_config_cache=C:\path\to\cache(Win)设置环境变量作为替代npm config set(这适用于 npm 中的所有配置选项)。
For anyone using docker you can add the env var at runtime with:
对于使用 docker 的任何人,您都可以在运行时添加 env var:
docker run -e npm_config_cache=/path/to/cache mydockerimage:tag
回答by Luke P. Issac
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:
对于 package.json 中的所有包,根据需要拥有缓存路径:
Just be in the directory where package.json is as usual and do
只需在 package.json 像往常一样的目录中执行
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?
你可能在 npm 文档中找不到这个,但我已经用 npm 6 尝试过它并且它有效。看起来它从 npm 5 开始工作 [参考:如何在安装命令的 npm5 中指定缓存文件夹?
回答by Stanley85
In Windows you can simply cd to the desired cache folder and do npm set cache --global
在 Windows 中,您可以简单地 cd 到所需的缓存文件夹并执行 npm set cache --global
回答by DoesEatOats
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.
最后,可以检查他们的 npm-cache 目录以查看是否正在填充。

