npm 查看源以及切换源

1. 设置为淘宝镜像

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

2. 设置回原本的源,用来发布npm包

npm config set registry https://registry.npmjs.org

3. 查看npm当前设置的源 

npm config get registry 或者 npm config list

### 切换 npm 的配置方法 在 npm切换可以通过多种方式实现,以下是常见的几种方法及其详细说明: #### 方法一:通过命令行直接设置 可以使用 `npm config set registry` 命令来永久更改 npm 的镜像。例如: - 切换到淘宝镜像: ```bash npm config set registry https://registry.npmmirror.com/ ``` 验证是否成功: ```bash npm config get registry ``` 如果返回值为 `https://registry.npmmirror.com/`,则表示切换成功[^1]。 - 切换到腾讯云镜像: ```bash npm config set registry http://mirrors.cloud.tencent.com/npm/ ``` 同样可以通过以下命令验证: ```bash npm config get registry ``` 如果返回值为 `http://mirrors.cloud.tencent.com/npm/`,则表示切换成功[^5]。 - 切换到华为云镜像: ```bash npm config set registry https://mirrors.huaweicloud.com/repository/npm/ ``` 验证命令与上述相同[^1]。 - 切换回官方: ```bash npm config set registry https://registry.npmjs.org/ ``` #### 方法二:临时使用某个镜像 如果仅希望在某次安装中使用特定的镜像,而不想更改全局配置,可以使用 `--registry` 参数。例如: ```bash npm install package-name --registry=https://registry.npmmirror.com/ ``` 这种方式不会影响全局配置,仅对当前命令生效[^4]。 #### 方法三:使用 nrm 工具管理镜像 nrm 是一个专门用于管理 npm 镜像的工具,可以更方便地切换镜像。 - 安装 nrm: ```bash npm install -g nrm ``` - 查看可用的镜像列表: ```bash nrm ls ``` - 切换到指定镜像: ```bash nrm use taobao ``` 或者: ```bash nrm use npm ``` 验证当前使用的镜像: ```bash npm config get registry ``` 需要注意的是,如果项目目录中存在 `.npmrc` 文件,并且文件中配置了 `registry`,那么该配置会优先于全局配置生效[^3]。 #### 方法四:通过 cnpm 工具安装依赖 除了直接切换镜像外,还可以使用淘宝提供的 `cnpm` 工具来安装依赖。安装 `cnpm` 的命令如下: ```bash npm install -g cnpm --registry=https://registry.npmmirror.com/ ``` 安装完成后,可以使用 `cnpm` 命令代替 `npm` 命令进行依赖安装。 ### 注意事项 - 在切换镜像后,建议通过 `npm config get registry` 命令确认当前使用的镜像是否正确。 - 如果遇到 `.npmrc` 文件优先级问题,可以删除或修改该文件以确保全局配置生效[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值