【Node.js】:配置源(registry)、代理(proxy)

--
created: 2025-07-08T09:27:40 (UTC +08:00)
tags: [npm,https,yarn,网络安全]
source: https://cloud.tencent.com/developer/article/1781066

【Node.js】:配置源(registry)、代理(proxy)

Excerpt

换npm、yarn的镜像源,或配置npm、yarn的代理,都是为了解决 npm 依赖下载慢的问题。


目录 1. 背景 2. npm 2.1. 配置镜像源 2.2. 配置代理 3. yarn 3.1. 配置镜像源 3.2. 配置代理

1. 背景

换npm、yarn的镜像源,或配置npm、yarn的代理,都是为了解决 npm 依赖下载慢的问题。

  • 如果你要下载的依赖,都能在“淘宝”或者“cnpm”镜像源上找到,那么换镜像源就能加速很多。
  • 如果你必须通过“npm镜像源”下载依赖(例如:你依赖的某特定版本的库,淘宝、cnpm镜像源上还没来得及与 npm 镜像源同步),那么你就需要配置代理科学上网(如果你有VPN,那更省事)。

2. npm

2.1. 配置镜像源

  • 方式1:npm 命令
// 查看镜像源 npm config get registry // 设置镜像源 npm config set registry http://registry.npm.taobao.org/ npm config set registry https://registry.npmjs.org/

  • 方式2:nrm 工具
    • nrm can help you easy and fast switch between different npm registries, now include: npm, cnpm, taobao, nj(nodejitsu).
npm install -g nrm // 安装 nrm ls // 查看已有的源 nrm use <registry> // 切换源 nrm add <registry> <url> // 添加源 nrm del <registry> // 删除源 nrm test [registry] // 测速

2.2. 配置代理

// 查看代理 npm config get proxy npm config get https-proxy // 设置代理 npm config set proxy http://127.0.0.1:8080 npm config set https-proxy http://127.0.0.1:8080 // 删除代理 npm config delete proxy npm config delete https-proxy

3. yarn

3.1. 配置镜像源

// 查看镜像源 yarn config get registry // 设置镜像源 yarn config set registry http://registry.npm.taobao.org/ yarn config set registry https://registry.npmjs.org/

3.2. 配置代理

// 查看代理 yarn config get proxy yarn config get https-proxy // 设置代理 yarn config set proxy http://127.0.0.1:8080 yarn config set https-proxy http://127.0.0.1:8080 // 删除代理 yarn config delete proxy yarn config delete https-proxy

参考:

NPM registry manager(nrm): https://github.com/Pana/nrm npm Docs: https://docs.npmjs.com/cli/v6/using-npm/config#proxy https://docs.npmjs.com/cli/v6/using-npm/config#https-proxy

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。

原始发表:2021-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息