使用 nrm 管理前端项目 nodejs 模块 npm 安装源(registry)地址
- 前端开发
- 2018-01-28
- 4654热度
- 0评论
无论使用 npm 还是 yarn 管理前端项目的依赖,都可能会遇到网络访问问题而头疼。另外,你可能工作中需要使用内部私有仓库模块,却又有时又需要对外发布或更新模块。此时频繁来回切换或加 registry 后缀也会比较烦人。
nrm 即是因此类需求而产生的,它是一个用于快速切换 npm 源地址的 nodejs 工具。
1. 使用 nrm
安装 nrm:
npm i -g @lzwme/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/
rednpm - http://registry.mirror.cqupt.edu.cn/
npmMirror - https://skimdb.npmjs.com/registry/
edunpm - http://registry.enpmjs.org/
我们可以添加内部私有的源到列表,以便于后续快速切换。示例:
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 <registry> Change registry to registry
add <registry> <url> [home] Add one custom registry
del <registry> Delete one custom registry
home <registry> [browser] Open the homepage of registry with optional browser
test [registry] Show response time for specific or all registries
help Print this help
如欲知更为详细的细节,可前往 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 config set chromedriver_cdnurl=http://npm.taobao.org/mirrors/chromedriver
npm config set puppeteer_download_host=https://npm.taobao.org/mirrors
也可以采用设置环境变量方式。例如在 linux/macOs 下:
export ELECTRON_MIRROR=http://npm.taobao.org/mirrors/electron/
export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
export NVM_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs
export NVMW_NPM_MIRROR=http://npm.taobao.org/mirrors/npm
export PYTHON_MIRROR=http://npm.taobao.org/mirrors/python
export SASS_BINARY_SITE=http://npm.taobao.org/mirrors/node-sass
export SQLITE3_BINARY_SITE=http://npm.taobao.org/mirrors/sqlite3
export PUPPETEER_DOWNLOAD_HOST=https://npm.taobao.org/mirrors
windows 下则可将 export
改为 set
,然后在 cmd 命令行中执行(当前命令行窗口有效),或者在用户 环境变量
中新增以上各项(全局永久有效)。
更多列表可前往淘宝 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