很多开发者会遇到这样的情况,Node安装包的时候,国内镜像源有些包安装不上,需要更换为淘宝镜像源,而有些包在淘宝镜像源又下载不到,导致需要每次重新设置镜像源,以至于下次都不知道当前使用的哪个镜像源故该管理工具出现,极大便利了我们的开发
(资料图片)
nrm是 Node.js 的镜像管理小工具,可以方便地查看镜像源列表和管理这些镜像源,并且可以快速地切换到最适合当前网络环境的镜像源。
2、安装全局安装 npm i nrm -g。
3、常用命令「查看」当前可用的镜像源列表:nrm ls。
「切换」镜像源 :nrm use
「添加」新的镜像源:nrm add
「删除」镜像源 :nrm del
「测试」镜像源速度:nrm test。
「显示」当前使用的镜像源:nrm current。
查看源列表时,当前源的名称前面带*号,也可以用于查看当前镜像源。打开指定镜像源的「网站首页」:nrm home
显示nrm命令的「帮助信息」:nrm help。
4、常见报错nrm ls 报错如下:
原因:由于 nrm 的依赖模块 open 采用的是 ES Module 的方式,但是 nrm 自身是一个 CommonJS 模块,无法直接加载 ES Module 的依赖。
解决:先注释掉报错文件的第9行(按照路径查找文件,如果使用的编辑器终端可以用 ctrl + 鼠标左键 点击报错文件直接跳转),再将 require("open") 改为 import("open"),具体修改如下:
// 找到 onHome 函数并修改 // 源代码 function onHome (name, browser) { var allRegistries = getAllRegistry(); var home = allRegistries[name] && allRegistries[name].home; if (home) { var args = [home]; if (browser) args.push(browser); open.apply(null, args); } } // 修改为 function onHome(name, browser) { var allRegistries = getAllRegistry(); var home = allRegistries[name] && allRegistries[name].home; if (home) { var args = [home]; if (browser) args.push(browser); import("open") .then((module) => { var open = module.default; open(...args); }) .catch((error) => { console.error(error); }); } }
nrm ls显示镜像源列表后,当前源的前面不带*号。
再次打开刚才的cli.js文件,修改代码如下,把&&修改为||,具体如下:// 源代码 config(attrs, registry).then(() => { console.log(" "); const newR = npm.config.get(FIELD_REGISTRY); var customRegistries = getCustomRegistry(); Object.keys(customRegistries).forEach(key => { delete customRegistries[key][FIELD_IS_CURRENT]; }); if (hasOwnProperty(customRegistries, name) && (name in registries || customRegistries[name].registry === registry.registry)) { registry[FIELD_IS_CURRENT] = true; customRegistries[name] = registry; } setCustomRegistry(customRegistries); printMsg(["", " Registry has been set to: " + newR, ""]); }).catch(err => { exit(err); }) // 修改后 config(attrs, registry).then(() => { console.log(" "); const newR = npm.config.get(FIELD_REGISTRY); var customRegistries = getCustomRegistry(); Object.keys(customRegistries).forEach(key => { delete customRegistries[key][FIELD_IS_CURRENT]; }); if (hasOwnProperty(customRegistries, name) || (name in registries || customRegistries[name].registry === registry.registry)) { registry[FIELD_IS_CURRENT] = true; customRegistries[name] = registry; } setCustomRegistry(customRegistries); printMsg(["", " Registry has been set to: " + newR, ""]); }).catch(err => { exit(err); })
标签:
每日快看:西峡法院召开十一月份审判执行工作讲评会
全球今日报丨威海广泰:公司高度重视特殊业务的发展,将进一步加强该业务的拓展
为什么家里的电器都是直流电 入户的却是交流电?
山西潞城:火红灯笼闹元宵 张灯结彩年味浓
滚动