npm 仓库源
查看和设置
# 查看
npm config get registry
# 设置
# 官方源
npm config set registry https://registry.npmjs.org
# 淘宝源
npm config set registry https://registry.npmmirror.com
# 腾讯源
npm config set registry https://mirrors.cloud.tencent.com/npm/
# 华为源
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
# 临时使用
npm install -g xxx --registry=https://registry.npmmirror.com
使用 nrm 管理
# 安装
npm install -g nrm
# 添加
# 官方源
nrm add npm https://registry.npmjs.org
# 淘宝源
nrm add taobao https://registry.npmmirror.com
# 腾讯源
nrm add tencent https://mirrors.cloud.tencent.com/npm/
# 华为源
nrm add huawei https://mirrors.huaweicloud.com/repository/npm/
# 查看
nrm ls
# 使用
nrm use taobao
多源
在 npm 中实现多源配置通常通过为不同 作用域(scope) 设置不同的镜像源来实现,这样不同来源的包可以自动从对应的镜像下载
# 为特定作用域(如 @company)设置私有源:
npm config set @company:registry https://private-registry.company.com
.npmrc 文件
.npmrc 文件是 npm 的配置文件,放在项目根目录或用户主目录中即生效
# 默认源(如淘宝镜像)
registry=https://registry.npmmirror.com
# 私有作用域对应源
@company:registry=http://company-registry.com
@another-scope:registry=https://another-registry.com
# 私有源认证令牌
//company-registry.com/:_authToken=your-token
优先级
npm 配置优先级:项目级 .npmrc > 用户级 ~/.npmrc > 全局配置。