Skip to content

nrm 常用命令

nrm(NPM Registry Manager)是一个 npm 镜像源管理工具,可以快速查看、切换、测试不同的 npm 镜像源。

💡 镜像源(Registry)是包管理器下载依赖的服务器地址。默认的 npm 官方源在国外,国内访问速度慢。国内镜像源(如淘宝镜像)是官方源的实时同步副本,使用国内服务器提供下载,速度更快。


安装

bash
# 全局安装
npm install -g nrm

# 查看版本
nrm --version
nrm -V

常用命令

查看镜像源

bash
# 查看所有可用镜像源
nrm ls

# 输出示例:
#   npm ---------- https://registry.npmjs.org/
#   yarn --------- https://registry.yarnpkg.com/
#   taobao ------- https://registry.npmmirror.com/
#   cnpm --------- https://r.cnpmjs.org/
#   npmMirror ---- https://skimdb.npmjs.com/registry/

💡 带 * 号的是当前使用的镜像源。

查看当前源

bash
# 查看当前使用的镜像源
nrm current

切换镜像源

bash
# 切换到淘宝镜像
nrm use taobao

# 切换回官方源
nrm use npm

# 切换到 cnpm
nrm use cnpm

# 切换到自定义镜像
nrm use company

测试镜像速度

bash
# 测试所有镜像源的响应速度
nrm test

# 输出示例:
#   npm ---- 800ms
#   yarn --- 500ms
#   taobao - 50ms
#   cnpm --- 100ms

# 测试指定镜像源的速度
nrm test taobao
nrm test npm

自定义镜像源

添加自定义镜像

bash
# 添加私有镜像源
nrm add <name> <url>

# 示例:添加公司私有镜像
nrm add company https://npm.company.com/

# 示例:添加私有 Registry
nrm add private https://registry.private.com/

删除自定义镜像

bash
# 删除已添加的自定义镜像
nrm del <name>

# 示例
nrm del company

查看镜像源主页

bash
# 打开镜像源的主页
nrm home taobao
nrm home npm

使用场景

临时切换镜像源(安装某个包很慢)

bash
# 测试哪个镜像最快
nrm test

# 切换到最快的镜像
nrm use taobao

# 安装完成后可以切回官方源
nrm use npm

公司私有镜像源

bash
# 添加公司私有 npm 镜像
nrm add company https://npm.company.com/

# 切换到公司镜像
nrm use company

# 安装公司内部包
npm install @company/utils

CI/CD 环境

在 CI/CD 中推荐直接配置,不依赖 nrm:

bash
npm config set registry https://registry.npmmirror.com

常用镜像源说明

名称地址说明
npmhttps://registry.npmjs.org/官方源,国外服务器
yarnhttps://registry.yarnpkg.com/Yarn 官方源
taobaohttps://registry.npmmirror.com/淘宝镜像,国内推荐
cnpmhttps://r.cnpmjs.org/cnpm 镜像
npmMirrorhttps://skimdb.npmjs.com/registry/npm 镜像

常用镜像源对比

镜像同步频率速度(国内)推荐度
npm 官方实时国外用户
taobao实时⭐⭐⭐ 国内推荐
cnpm实时⭐⭐
npmMirror实时中等

nrm 与 .npmrc 的关系

nrm use 修改的是全局 npm 配置(~/.npmrc),项目级 .npmrc 可以覆盖全局配置:

bash
# 项目级 .npmrc 优先级更高
# 项目根目录/.npmrc
registry=https://registry.npmmirror.com

优先级顺序

项目级 .npmrc > 用户级 .npmrc(nrm 修改的)> 全局级 .npmrc

nrm 的局限性

只管理 npm 镜像源

nrm 只管理 npm 的镜像源,不影响 yarn 和 pnpm。它们各自有独立的配置:

bash
# yarn 设置镜像源(Classic 1.x)
echo 'registry "https://registry.npmmirror.com"' > .yarnrc

# yarn 设置镜像源(Berry 2.x+)
yarn config set npmRegistryServer https://registry.npmmirror.com

# pnpm 设置镜像源
pnpm config set registry https://registry.npmmirror.com

不支持 token 管理

nrm 只切换 registry URL,不管理认证 token。如果私有仓库需要认证,需手动配置 .npmrc

bash
# .npmrc
//registry.npmjs.org/:_authToken=your-token
//npm.company.com/:_authToken=company-token

常见问题

nrm ls 报错

bash
# 如果报错,可能是 nrm 版本问题,尝试重新安装
npm uninstall -g nrm
npm install -g nrm

切换后 npm 命令仍走旧源

nrm use 修改的是全局 npm 配置,如果项目中有 .npmrc 文件会覆盖全局配置:

bash
# 查看当前生效的镜像源
npm config get registry

# 检查是否有项目级 .npmrc
cat .npmrc

nrm add 添加的源不显示

bash
# 确认添加成功
nrm ls

# 如果不显示,可能是 nrm 版本问题
# 尝试更新 nrm
npm update -g nrm

与 npm config set 的区别

bash
# nrm use 修改的是 ~/.npmrc
nrm use taobao

# 等价于
npm config set registry https://registry.npmmirror.com

# 区别:nrm 可以快速在多个源之间切换
# npm config set 需要手动输入完整 URL

参考

个人学习笔记,部分内容借助 AI 辅助整理,仅供查阅参考,请以官方文档为准