如何从npm代理中排除某些域

  • 本文关键字:排除 代理 npm proxy npm
  • 更新时间 :
  • 英文 :


我们在公司防火墙后面使用npm,因此在npm配置中使用代理和https代理设置。只要所有npm模块都是从外部注册表加载的,这就可以正常工作。但事实上,我们在内部github上也有内部模块。对这些模块的访问当然不能使用代理。我的问题是:我可以在npm配置中指定一个不应该使用代理的域列表吗?像unix shell中的no_proxy环境变量?

假设您的环境如下所示:

  • 生成仅通过代理访问internet的服务器:your.proxy.host:3128
  • 本地Nexus注册:https://your.local.nexus.registry/nexus/content/groups/npm/

NPM必须使用本地Nexus注册表。配置文件:.npmrc

registry = https://your.local.nexus.registry/nexus/content/groups/npm/

您可以通过设置环境变量来告诉npm使用代理

http_proxy=http://your.proxy.host:3128
https_proxy=http://your.proxy.host:3128

但npm也会尝试使用代理访问您的(本地)Nexus注册表。

你需要有一个最新的npm版本(npm 2.14.7运行良好),并设置一个额外的环境变量,以将你的Nexus注册表从代理中排除:

no_proxy=your.local.nexus.registry

由于NPM 6.4.1于2018-08-22发布,即使配置了自定义注册表,您也可以使用noproxy选项。

示例:

  • npm config set registry "http://<my-npm-registry-host>:<registry-port>"
  • npm config set proxy "http://<my-proxy-host>:<proxy-port>"
  • npm config set https-proxy "http://<my-proxy-host>:<proxy-port>"
  • npm config set noproxy "my-proxy-host"(接受类似*.domain的模式)

检查配置:

  • npm config list

参考文献:

  • noproxy的NPM文档
  • NPM变更日志
  • NPM PR#46

要排除某些域,可以在.npmrc 中添加以下行

noproxy[]=.local.nexus
noproxy[]=.internal.npm.repo 

https://docs.npmjs.com/misc/config#noproxy

正如adiesner所指出的,您不能在npm config中设置noproxy/no_proxy,因此唯一的方法是通过环境变量:

npm config set registry "your.local.nexus.registry/nexus/content/groups/npm/"
export http_proxy="http://your.proxy.host:3128"
export https_proxy=$http_proxy
export no_proxy="your.local.nexus.registry"

如果您使用cntlm,您应该在配置文件中配置NoProxy选项"etc/ctlm.conf";。例如,

Username         your_user
Domain           UCI.CU
Password         your_password
Proxy            10.0.0.1:8080
Listen           3128
NoProxy          uci.cu, edu.cu, nexus.prod.uci.cu, localhost, 127.0.0.*, 10.*, 192.168.*

请记住,必须重新启动cntlm:sudo systemctl restart cntlm

对于PowerShell,命令为:

$Env:http_proxy = "http://your.proxy.host:3128"
$Env:https_proxy = "http://your.proxy.host:3128"
$Env:no_proxy = "..."

最新更新