使用npm仓库的优先级以及.npmrc配置文件的使用

说明

一、文件位置
全局配置文件:/etc/npmrc
用户配置文件:~/.npmrc
项目配置文件:$项目根目录/.npmrc

通过 npm config 修改的是用户配置文件~/.npmrc

// 本次从淘宝仓库源下载
npm install xx --registry=https://registry.npm.taobao.org install

// 设置淘宝源
npm config set registry https://registry.npm.taobao.org

// 设置公司的源
npm config set registry http://127.0.0.1:4873

// 查看当前源
npm config get registry

.npmrc文件配置

设置仓库

registry=https://registry.npm.taobao.org/

根据scope设置仓库:

@cmvalue:registry=http://npm.cmvalue.net/

配置bin目录:

prefix=/usr/local/npm

配置node-sass等模块仓库:

sass_binary_site=https://repo.huaweicloud.com/node-sass
phantomjs_cdnurl=https://repo.huaweicloud.com/phantomjs
chromedriver_cdnurl=https://repo.huaweicloud.com/chromedriver
operadriver_cdnurl=https://repo.huaweicloud.com/operadriver

自定义缓存目录:

cache=~/.cache/npm_cache

配置仓库认证信息,用于私有仓库读取或上传npm包:

always-auth=true
_auth="用户名:密码"的base64编码

在多私库且用户名密码不同的情况下,可以对单个私库配置_auth:

# :前为仓库地址去掉http/https
//repo.huaweicloud.com/repository/npm/:_auth=xxxxxx

除了使用用户名密码,还可以通过配置_authToken配置认证信息:

_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

举例:
自动切换npm源

@cmvalue:registry=http://npm.cmvalue.net/
registry=https://registry.npm.taobao.org/

如何配置发布组件

npm配置组件发布的方式有两种,一种是通过配置packege.json实现,另一种则是通过配置文件.npmrc实现。

package.json配置方式:

# @aa是组件的scope。scope在模块名name中使用时,以@开头,后边跟一个/
{
        "name": "@aa/xxx", // 发布npm包的名字
        "version": "1.0.0", // 你的npm包版本
        "description": "xxxx", // 包的描述
        "main": "dist/btn.js", // 指定组件的主入口文件
        "publishConfig": {
            "registry": "要发布的私有仓库地址,然后在.npmrc配置用户名密码"
        }
        ......
}

.npmrc配置方式:

# package.json不做任何仓库的配置:
{
        "name": "@aa/xxx", // 发布npm包的名字
        "version": "1.0.0", // 你的npm包版本
        "description": "xxxx", // 包的描述
        "main": "dist/btn.js", // 指定组件的主入口文件
        ......
}

# .npmrc配置仓库地址和用户名密码:
@aa:registry=私仓地址

配置好仓库信息后,执行发布命令即可将打包好的组件发布到仓库中:

npm publish

参考资料

官网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lvan的前端笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值