跳转nrm的安装目录,这里以~/.nvm/versions/node/v14.21.3/lib/node_modules/nrm/为例:

cd ~/.nvm/versions/node/v14.21.3/lib/node_modules/nrm/

使用vim修改cli.js,搜索onUse方法(按/进入搜索模式,输入onUse (,按回车使光标定位到指定位置),删除hasOwnProperty(customRegistries, name)这部分条件,修改完成后按:wq保存退出,如下所示:

function onUse (name) {
var allRegistries = getAllRegistry();
if (hasOwnProperty(allRegistries, name)) {
getCurrentRegistry(function (cur) {
let currentRegistry, item;
for (let key of Object.keys(allRegistries)) {
item = allRegistries[key];
if (item[FIELD_IS_CURRENT] && equalsIgnoreCase(item.registry, cur)) {
currentRegistry = item;
break;
}
}
var registry = allRegistries[name];
let attrs = [].concat(REGISTRY_ATTRS).concat();
for (let attr in Object.assign({}, currentRegistry, registry)) {
if (!REGISTRY_ATTRS.includes(attr) && !IGNORED_ATTRS.includes(attr)) {
attrs.push(attr);
}
}

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)) {
if (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);
});
});
} else {
printMsg(['', ' Not find registry: ' + name, '']);
}
}

再次使用nrm use taobao后即可生效。