在 Node.js 开发中,使用阿里云镜像可以显著提升依赖包的下载速度。以下是推荐的阿里云镜像配置方法:
1. Node.js 二进制镜像(安装 Node.js 本身)
- 镜像地址:https://mirrors.aliyun.com/nodejs-release/
- 适用场景:下载 Node.js 安装包(如通过
nvm或直接下载)。 -
配置方法:
# 使用 nvm 时指定镜像 NVM_NODEJS_ORG_MIRROR=https://mirrors.aliyun.com/nodejs-release/ nvm install 16.14.2 # 或直接下载(替换版本号) wget https://mirrors.aliyun.com/nodejs-release/v16.14.2/node-v16.14.2-linux-x64.tar.gz
2. npm 镜像(安装依赖包)
- 镜像地址:https://registry.npmmirror.com
- 适用场景:提速
npm install或yarn install。 -
配置方法:
# 临时使用 npm install --registry=https://registry.npmmirror.com # 永久配置 npm config set registry https://registry.npmmirror.com # 恢复默认 npm config set registry https://registry.npmjs.org
3. Yarn 镜像
- 镜像地址:https://registry.npmmirror.com
- 配置方法:
yarn config set registry https://registry.npmmirror.com
4. PNPM 镜像
- 镜像地址:https://registry.npmmirror.com
- 配置方法:
pnpm config set registry https://registry.npmmirror.com
5. 其他工具镜像
- Electron 镜像:
解决 Electron 二进制下载慢的问题:npm config set ELECTRON_MIRROR https://mirrors.aliyun.com/electron/
注意事项:
- 镜像同步延迟:阿里云镜像每隔 10 分钟同步一次官方源,通常无需担心时效性。
- 私有依赖:如果项目依赖私有包,需确保镜像支持或切换回官方源。
- 安全提示:推荐使用
https协议(如https://registry.npmmirror.com)。
通过以上配置,可以大幅提升 Node.js 开发中的依赖安装效率。遇到问题时,可尝试清除缓存后重试:
npm cache clean --force
云服务器