说明
一、文件位置
全局配置文件:/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