跳到主要内容

设置 npm 的 registry

使用 npm

原 npm 镜像地址

> npm config set registry http://registry.npmjs.org

设置国内镜像

通过 config 命令

> npm config set registry https://registry.npmmirror.com
> npm info underscore #(如果上面配置正确这个命令会有字符串 response)

命令行指定

> npm --registry https://registry.npmmirror.com info underscore

使用 yarn

Yarn 是 Facebook 最近发布的一款依赖包安装工具

安装

npm install -g yarn --registry=https://registry.npmmirror.com

安装时如果没有切换源,可以通过下边方式修改

# 查询当前 yarn 的 registry
> yarn config get registry

# 设置淘宝镜像源
> yarn config set registry https://registry.npmmirror.com

查询 yarn 安装依赖的全局路径

> yarn global bin

修改 yarn 全局 bin 位置

> yarn config set prefix "D:\nodejs\node_modules\yarn\bin"

修改 yarn 全局安装位置

> yarn config set global-folder "D:\nodejs\node_modules\yarn\node_modules"

修改 yarn 全局 cache 位置

> yarn config set cache-folder "D:\nodejs\node_modules\yarn\cache"

修改 yarn 全局 link 位置

> yarn config set link-folder "D:\nodejs\node_modules\yarn\data_link"

使用 nrm 管理 registry 地址

nrm 是一个管理 npm 源的工具。nrm 提供了一个命令行工具,可以方便地切换不同的 npm 源,在不同的场景下使用不同的源。使用 nrm 可以列出当前已有的所有源并切换到指定源,也可以添加自定义的源,并在多个源之间轻松切换。

安装 nrm

> npm install -g nrm

查看 nrm 版本

> nrm --version
1.2.6

添加 registry 地址

> nrm add taobao https://registry.npmmirror.com

查看 registry 地址

> nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
* taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/

切换 npm registry 地址

> nrm use taobao
SUCCESS The registry has been changed to 'taobao'.

搜索镜像: https://npmmirror.com/

建立或使用镜像,参考: https://github.com/cnpm/cnpmjs.org