npm 更改为淘宝镜像的方法[通俗易懂]

发布时间:2025-12-09 13:47:50 浏览次数:6

npm 更改为淘宝镜像的方法

1、命令行临时使用指定镜像(淘宝)

npm --registry https://registry.npm.taobao.org install express

2、命令行永久更改使用指定镜像(淘宝)

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

以后 npm install express 默认使用指定(淘宝)镜像

3、通过npm配置文件直接修改,本质和第2条一样,配置文件位置(windows环境)为C盘下的.npmrc文件(百度很容易查到文件路径),MAC没试过。可以用如下命令找到配置文件位置

npm config ls -l

4、使用淘宝 NPM 镜像(参考 http://www.runoob.com/nodejs/nodejs-npm.html)

命令行输入

npm install -g cnpm --registry=https://registry.npm.taobao.org

这样就可以使用 cnpm 命令来安装模块了:cnpm install express

查看目前使用的npm镜像的方法:

npm config get registry

5、设置单独某个包的下载地址(以包electron,其镜像https://npm.taobao.org/mirrors/electron/为例,参考https://reactnative.cn/docs/debugging/)

打开.npmrc,添加该包的指定镜像,截图如下

6、要想将镜像源改回国外的源可以直接将.npmrc文件直接删除,想恢复时直接从回收站还原即可。

7、查看npm源上包的所有版本,以babel-core为例:

npm view babel-core versions

8、安装包的最新版本,包含beta版,以babel-core为例:

npm install babel-core@next

9、查看package-lock.json中具体依赖树(以开源库tar为例):

npm ls tar

npm的缓存及全局包位置(默认情况)

可以通过命令行”npm config get cache”获取缓存目录,对于全局路径还可以用这个命令”npm root -g”

mac下:/Users/apple/.npmrc (“apple”是自己的mac用户名)

window下:%APPDATA%/npm/node_modules

npm源管理工具:

nrm:npm install -g nrm

参考:

1、https://www.jianshu.com/p/e953bd426368

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477