[ npm ] 配置与Registry管理

本文介绍了如何配置npm的Registry,包括三种不同的方法:直接设置命令、使用nrm管理工具等。通过这些方法,可以方便地在不同Registry之间切换,尤其对于需要在npm官方源和国内镜像之间频繁切换的开发者来说十分实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

npm config

首先使用 npm config -h ,查看它的相关信息以及命令行。

npm config set <key> <value>  # 设置 npm 配置
npm config get [<key>]        # 获取 npm 指定配置的值
npm config delete <key>       # 删除 npm 指定配置
npm config list [--json]      # 列出 npm 的配置[可以以json格式输出]
npm config edit               # 编辑 npm 配置
npm set <key> <value>
npm get [<key>]

alias: c    # 别称,你可以用 "npm c" 等同 "npm config"

配置 Registry

方法1:

设置国内镜像:

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

设置原npm地址:

npm config set registry http://registry.npmjs.org 
方法2:
npm config edit
# 进入编辑模式
;;;;
; npm userconfig file
; this is a simple ini-formatted file
; lines that start with semi-colons are comments.
; read `npm help config` for help on the various options
;;;;
registry=http://registry.npmjs.org/
方法3:

使用 nrm 管理 registry 地址

  1. 安装

    npm install -g nrm
    
  2. 添加 registry 地址

    nrm add npm http://registry.npmjs.org
    nrm add taobao https://registry.npm.taobao.org
    
  3. 切换 npm registry 地址

    nrm use taobao
    nrm use npm
    

    除此之外,nrm还可以管理多个第三方包管理工具的registry

    $ nrm ls # 查看所有第三方包管理工具的registry
    
    * npm -----  https://registry.npmjs.org/
      yarn ----- https://registry.yarnpkg.com
      cnpm ----  http://r.cnpmjs.org/
      taobao --  https://registry.npm.taobao.org/
      nj ------  https://registry.nodejitsu.com/
      skimdb -- https://skimdb.npmjs.com/registry
    
    
    $ nrm use cnpm  # 切换 cnpm 的 registry
    
    Registry has been set to: http://r.cnpmjs.org/
    

后记

因为本人有在尝试将包发布到npm,但是由于国内环境一般都是将源设置为淘宝镜像,但是这个registry的情况是不能发布的。名称都要手动切换到npm官方源发布后,再切换回来。因此寻找到一个方便切换,指令好记的管理Registry的包。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值