无论使用 npm 还是 yarn 管理前端项目的依赖,都可能会遇到网络访问问题而头疼。另外,你可能工作中需要使用内部私有仓库模块,却又有时又需要对外发布或更新模块。此时频繁来回切换或加 registry 后缀也会比较烦人。
nrm 即是因此类需求而产生的,它是一个用于快速切换 npm 源地址的 nodejs 工具。
1. 使用 nrm
安装 nrm:
npm i -g nrm
nrm 通过别名简写的方式让你能够快速切换 registry。它默认设置了一些常用源的简写。
nrm ls
得到默认定义的源列表:
npm ---- https://registry.npmjs.org/
cnpm --- http://r.cnpmjs.org/
taobao - https://registry.npm.taobao.org/
nj ----- https://registry.nodejitsu.com/
npmMirror - https://skimdb.npmjs.com/registry/
我们可以添加内部私有的源到列表,以便于后续快速切换。示例:
nrm add lzwme https://registry.lzw.me https://npm.lzw.me
nrm add yarn https://registry.yarnpkg.com https://yarnpkg.com/zh-Hans/
nrm use lzwme
更多 nrm 命令参考:
ls List all the registries
current Show current registry name
use Change registry to registry
add [home] Add one custom registry
del Delete one custom registry
home [browser] Open the homepage of registry with optional browser
test [registry] Show response time for specific or all registries
help Print this help
如欲知更为详细的细节,可前往 nrm 项目地址了解:
- nrm [https://github.com/Pana/nrm](https://github.com/Pana/nrm)
## 2. 使用淘宝镜像地址
一些模块在安装过程中还需要实时下载二进制的资源,这个地址是模块内默认定义的,可不在 registry 可控的范围内。不过一般他们都会提供环境变量配置指定安装来源,而淘宝仓库提供了一些流行模块的镜像源,这使得我们可以较为顺利地安装上这些模块。
使用淘宝镜像源地址参考:
npm config set registry https://registry.npm.taobao.org
npm config set disturl https://npm.taobao.org/dist
npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/
更多列表可前往淘宝 npm 页面参考:https://npm.taobao.org
## 3. 相关 URL 参考
npmconfig https://docs.npmjs.com/misc/config
nrm https://github.com/Pana/nrm
npm https://www.npmjs.org
cnpm http://cnpmjs.org
taobao https://npm.taobao.org
nvm https://github.com/creationix/nvm
领取专属 10元无门槛券
私享最新 技术干货