|
| 1 | +# NPM 镜像 |
| 2 | + |
| 3 | +在使用 [npm](https://www.npmjs.com/) 过程中经常会遇到无法下载包的问题,通常是由于网络原因导致的。为了解决这个问题,我们可以使用 npm 镜像来加速下载。 |
| 4 | + |
| 5 | +## 基本命令 |
| 6 | + |
| 7 | +镜像的基本操作包括查看镜像、设置镜像和取消镜像。 |
| 8 | + |
| 9 | +### 查看镜像 |
| 10 | + |
| 11 | +::: code-group |
| 12 | +```bash [node] |
| 13 | +npm config get registry |
| 14 | +``` |
| 15 | + |
| 16 | +```bash [yarn] |
| 17 | +yarn config get registry |
| 18 | +``` |
| 19 | +::: |
| 20 | + |
| 21 | +### 设置镜像 |
| 22 | + |
| 23 | +以阿里云镜像为例。 |
| 24 | + |
| 25 | +#### 全局 |
| 26 | + |
| 27 | +设置镜像命令如下: |
| 28 | + |
| 29 | +::: code-group |
| 30 | +```bash [npm] |
| 31 | +npm config set registry https://registry.npmmirror.com |
| 32 | +``` |
| 33 | + |
| 34 | +```bash [yarn] |
| 35 | +yarn config set registry https://registry.npmmirror.com |
| 36 | +``` |
| 37 | +::: |
| 38 | + |
| 39 | + |
| 40 | +#### 临时 |
| 41 | + |
| 42 | +使用下面的命令安装包时可以临时设置镜像,只对当前命令有效。 |
| 43 | + |
| 44 | +::: code-group |
| 45 | +```bash [npm] |
| 46 | +npm install --registry https://registry.npmmirror.com packages |
| 47 | +``` |
| 48 | +```bash [yarn] |
| 49 | +yarn add --registry https://registry.npmmirror.com packages |
| 50 | +``` |
| 51 | +::: |
| 52 | + |
| 53 | +### 重置镜像 |
| 54 | + |
| 55 | +使用下面的命令可以重置镜像为 `npm` 默认的 [registry.npmjs.com](https://registry.npmjs.org/) 镜像,`yarn` 默认的 [registry.yarnpkg.com](https://registry.yarnpkg.com) 镜像: |
| 56 | + |
| 57 | +::: code-group |
| 58 | +```bash [npm] |
| 59 | +npm config delete registry |
| 60 | +``` |
| 61 | +```bash [yarn] |
| 62 | +yarn config delete registry |
| 63 | +``` |
| 64 | +::: |
| 65 | + |
| 66 | +## 相关命令 |
| 67 | + |
| 68 | +| 功能/工具名称 | yarn | npm | |
| 69 | +|---------|------------------------------------------------------------------------------|--------------------------| |
| 70 | +| 安装所有依赖 | `yarn` / `yarn install` | `npm install` | |
| 71 | +| 添加依赖 | `yarn add packages` | `npm install packages` | |
| 72 | +| 删除依赖 | `yarn remove packages` | `npm uninstall packages` | |
| 73 | +| 更新依赖 | `yarn upgrade`<br />`yarn upgrade --registry https://registry.npmmirror.com` | `npm update` | |
| 74 | +| 搜索软件包 | `—` | `npm search packages` | |
| 75 | +| 清空缓存 | `yarn cache clean -f` | `npm cache clean -f` | |
| 76 | + |
| 77 | +## 国内镜像地址 |
| 78 | + |
| 79 | +下面整理一些国内常用的镜像地址。 |
| 80 | + |
| 81 | +### 阿里云镜像 |
| 82 | + |
| 83 | +镜像地址:`https://registry.npmmirror.com` |
| 84 | + |
| 85 | +文档地址:[npmmirror 镜像站](https://npmmirror.com/) |
| 86 | + |
| 87 | +> 实时同步自 [npmjs.com 官方](https://www.npmjs.com/) |
| 88 | +
|
| 89 | +::: details `.yarnrc` / `.npmrc` 配置 |
| 90 | +```text |
| 91 | +registry "https://registry.npmmirror.com" |
| 92 | +
|
| 93 | +sass_binary_site "https://registry.npmmirror.com/-/binary/node-sass/" |
| 94 | +electron_mirror "https://registry.npmmirror.com/-/binary/electron/" |
| 95 | +sqlite3_binary_host_mirror "https://registry.npmmirror.com/-/binary/sqlite3/" |
| 96 | +profiler_binary_host_mirror "https://registry.npmmirror.com/-/binary/node-inspector/" |
| 97 | +chromedriver_cdnurl "https://registry.npmmirror.com/-/binary/chromedriver" |
| 98 | +sentrycli_cdnurl "https://registry.npmmirror.com/-/binary/sentry-cli" |
| 99 | +``` |
| 100 | +::: |
| 101 | + |
| 102 | +### 腾讯云镜像 |
| 103 | + |
| 104 | +镜像地址:`https://mirrors.cloud.tencent.com/npm/` |
| 105 | + |
| 106 | +文档地址:[腾讯云 npm 镜像](https://mirrors.cloud.tencent.com/help/npm.html) |
| 107 | + |
| 108 | +> 同步频率为每天一次,同步的时间为凌晨0点-2点。 |
| 109 | +
|
| 110 | +### 华为云镜像 |
| 111 | + |
| 112 | +镜像地址:`https://mirrors.huaweicloud.com/repository/npm/` |
| 113 | + |
| 114 | +文档地址:[华为云 npm 镜像](https://mirrors.huaweicloud.com/help/npm.html) |
| 115 | + |
| 116 | +> 同步频率不详 |
0 commit comments