[npm源管理器nrm使用教程]

# [npm源管理器nrm使用教程]

# 1. nrm介绍

m包有很多的镜像源,有的源有的时候访问失败,有的源可能没有最新的包等等,所以有时需要切换npm的源,nrm包就是解决快速切换问题的。 nrm可以帮助您在不同的npm源地址之间轻松快速地切换。

nrm内置了如下源:

URL 主页
npm https://registry.npmjs.org/ (opens new window) https://www.npmjs.com/ (opens new window)
cnpm http://r.cnpmjs.org/ (opens new window) https://cnpmjs.org/ (opens new window)
taobao https://registry.npm.taobao.org/ (opens new window) https://npm.taobao.org/ (opens new window)
npmMirror https://skimdb.npmjs.com/regi... (opens new window) https://skimdb.npmjs.com/ (opens new window)
nj https://registry.nodejitsu.com/ (opens new window) https://www.nodejitsu.com/ (opens new window)
rednpm http://registry.mirror.cqupt.... (opens new window) http://npm.mirror.cqupt.edu.cn/ (opens new window)
edunpm http://registry.enpmjs.org/ (opens new window) http://www.enpmjs.org/ (opens new window)

经过本人实测,nj、rednpm、edunpm 源访问失败(2018-12-19)。

# 2. 安装:npm install -g nrm

打开终端运行npm install -g nrm命令:

npm install nrm -g

查看是否安装成功:

nrm --version

# 3. 使用

# 3.1 列出可选择的源:nrm ls

~ 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/

*注:* 前面带 * 号的表示正在使用的源

# 3.2 切换使用的源:nrm use npm

~ nrm use npm
                        
   Registry has been set to: https://registry.npmjs.org/

# 3.3 添加一个源:nrm add

如果你想添加一个源,终端执行命令nrm add <registry> <url> [home],reigstry为源名,url为源的路径, home为源的主页(可不写)。

 ~ nrm add company http://npm.company.com/   

    add registry company success

*注:*

  1. URL最后的/也可以不带,下面两个URL都是可以的: http://npm.company.com/ http://npm.company.com
  2. [home]参数用于nrm home命令,用来查看源的主页。

# 3.4 删除一个源:nrm del

想要删除一个源,终端执行命令nrm del <registry>,reigstry为源名.

 ~ nrm del company

    delete registry company success

***注:***nrm del 命令不能删除nrm自己内置的源。

# 3.5 测试源速度:nrm test

测试一个源的响应时间:nrm test npm

~ nrm test npm

* npm ---- 833ms

测试所有源的速度:nrm test

~ nrm test

* npm ---- 807ms
  cnpm --- 374ms
  taobao - 209ms
  nj ----- Fetch Error
  rednpm - Fetch Error
  npmMirror  1056ms
  edunpm - Fetch Error

# 3.6 访问源的主页:nrm home taobao

如果你想访问源的主页,可在终端输入下面命令:

nrm home taobao

此命令会在浏览器中打开淘宝源的主页:https://npm.taobao.org/ (opens new window)

*注:* 如果要查看自己添加的源的主页,那么在添加源的时候就要把主页带上:

~ nrm add company http://npm.company.com/ http://npm.company.com/

如果添加源的时候没有写home信息,那么nrm home命令不会有效果。

# 4. 不使用nrm来切换源

如果不是nrm也能切换源,只不过比较麻烦。

查看当前使用的源

npm config get registry

~ npm config get registry
https://registry.npmjs.org/

设置一个源

~ npm config set registry https://registry.npm.taobao.org/

设置成功后终端不会有任何输出

安装包使用特定源 全部使用特定源安装:npm install --registry=https://registry.npm.taobao.org 安装一个包使用特定源:npm i logo --registry=https://registry.npm.taobao.org

#

# 安装错误1:nrm 安装成功之后 提示“不是内部或外部命令……”

在命令行执行命令,npm install -g nrm,全局安装nrm。执行nrm -v之后就说不是内部命令

# 解决办法:

1、查看nodejs全局安装路径:npm config ls

img

其中 prefix 的路径就是 nodejs全局安装路径。

2、将nodejs全局安装路径 配置到系统环境变量中PATH中即可 路径为 nodejs全局安装路径(我这里值为D:\node.js\node_global

img

img

# 启动cmd,输入 nrm ls

1.如成功则会显示下列目录

img

# 如果你是bug工程师,恭喜你,遇到了这个:

img

# 解决办法:

1、首先检查node.js是否安装成功,输入 node -v 若可查看版本号,如图所示即安装成功;

若不一致则重新安装node.js。

node.js官方下载地址:https://nodejs.org/en/download/

img

2.查看npm是否安装成功,如下图成功,反之则重新安装

img

3、报错截图中可见 cli.js文件中 第17行报错,

img

按路径找到该文件:

img

打开文件找到报错的第17行,注掉原17行改为如图:

//const NRMRC = path.join(process.env.HOME, '.nrmrc');(注掉)

const NRMRC = path.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], '.nrmrc');

再管理员模式运行cmd,输入nrm ls :

img