在 Node.js 开发中,npm registry 是用于存储和管理Node.js包的地方。随着项目的增长和依赖项的增加,管理 npm registry 可能会变得非常复杂。这时,nrm(npm registry manager)就派上了用场。
nrm 是一个简单易用的命令行工具,用于管理多个 npm registry。它可以帮助开发人员快速切换不同的 registry,并简化管理流程。
- 项目官网:https://github.com/Pana/nrm
要知道,在国内访问默认的npm源是相当慢的,有头疼的问题,就会有解决方案,之前是通过npm手动配置源,像下面这样。
npm set registry https://registry.npm.taobao.org/
再 npm config ls 查看,已经切换成功。够用,但还是挺麻烦的,看看 nrm 是怎么使用的吧!
一、安装 nrm
首先,你需要安装nrm。你可以使用 npm 全局安装 nrm:
npm install -g nrm
二、使用 nrm
安装完成后,你可以在命令行中使用 nrm 命令来管理 npm registry。
$ nrm --help
Usage: nrm [options] [command]
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
ls List all the registries
current [options] Show current registry name or URL
use <name> Change current registry
add <name> <url> [home] Add custom registry
login [options] <name> [base64] Set authorize information for a custom registry with a base64 encoded string or username and password
set-hosted-repo <name> <repo> Set hosted npm repository for a custom registry to publish package
set-scope <scopeName> <url> Associating a scope with a registry
del-scope <scopeName> Remove a scope
set [options] <name> Set a custom registry attribute
rename <name> <newName> Change custom registry name
del <name> Delete custom registry
home <name> [browser] Open the homepage of registry with optional browser
test [registry] Show response time for specific or all registries
help [command] display help for command
常用命令说明:
# 查看镜像列表
nrm ls
# 查看当前使用的镜像
nrm current
# 添加镜像
nrm add <名称> <远程地址或私服地址>
# 删除镜像
nrm del <名称>
# 切换镜像
nrm use <名称>
# 测试镜像网络传输速度
nrm test <名称>
# 查看nrm版本号
nrm <-version | -V>
# 查看nrm相关信息
nrm <-help | -h>
# 打开镜像主页
nrm home <名称> [browser]
# 上传npm包或命令程序
nrm publish [<tarball>|<folder>]
使用 nrm ls
查看支持的 nrm registry:
$ nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
* taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
切换到淘宝提供的镜像:
$ nrm use taobao
真的很简单!
三、注意事项
在使用 nrm 时,需要注意:nrm 会将默认的 npm registry(通常为npmjs.org)作为默认的 fallback registry。这意味着在切换到其他 registry 时,如果该 registry 没有你要安装的包,nrm 会自动切换回默认的 fallback registry 来获取包。你可以通过设置环境变量 NRM_FORCE_FALLBACK
为 false
来禁用此行为。