切换到国内的 npm 源可以提高依赖安装的速度,特别是在国内网络环境下常使用 npm
或 yarn
时。以下是切换 npm 源的常用方法:
npm
配置国内源使用以下命令将 npm 的默认源切换为国内镜像(如淘宝镜像):
npm config set registry https://registry.npmmirror.com/
验证是否设置成功:
npm config get registry
输出应为:
https://registry.npmmirror.com/
如果只想临时切换源安装一个包,可以在安装命令前添加 --registry
参数:
npm install package-name --registry=https://registry.npmmirror.com/
cnpm
是淘宝 NPM 镜像的命令行工具,安装后可以更方便地使用国内源。
cnpm
:npm install -g cnpm --registry=https://registry.npmmirror.com/
cnpm
安装依赖:cnpm install
cnpm
会自动使用淘宝源,无需额外配置。
nrm
是一个管理和切换 npm 源的工具,支持多源切换,非常方便。
nrm
:npm install -g nrm
nrm ls
输出示例:
* npm -------- https://registry.npmjs.org/ yarn ------- https://registry.yarnpkg.com/ taobao ----- https://registry.npmmirror.com/
nrm use taobao
nrm test
nrm use npm
如果使用的是 Yarn,可以通过以下命令切换到国内源:
yarn config set registry https://registry.npmmirror.com/
验证是否设置成功:
yarn config get registry
https://registry.npmmirror.com/
)。nrm
,管理和切换更方便。npm cache clean --force
本文作者:Kevin@灼华
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!