您现在的位置是:网站首页 > NPM私有仓库配置文章详情
NPM私有仓库配置
陈川
【
Node.js
】
31745人已围观
4248字
为什么需要NPM私有仓库
企业内部开发时,经常需要共享一些私有模块。这些模块可能包含敏感代码或业务逻辑,不适合发布到公共NPM仓库。搭建私有NPM仓库可以解决以下问题:
- 安全地共享公司内部模块
- 控制依赖包的访问权限
- 加速内部模块的安装速度
- 避免公共NPM服务不稳定带来的影响
常见NPM私有仓库解决方案
Verdaccio
Verdaccio是一个轻量级的私有NPM代理注册表,具有以下特点:
- 零配置即可运行
- 支持缓存公共包
- 提供Web界面
- 支持插件扩展
安装Verdaccio非常简单:
npm install -g verdaccio
运行后默认监听4873端口:
verdaccio
Nexus Repository
Nexus是一个功能更强大的仓库管理工具:
- 支持多种包格式(NPM、Docker、Maven等)
- 提供细粒度的权限控制
- 支持高可用部署
- 企业级功能更丰富
GitHub Packages
GitHub提供的包管理服务:
- 与GitHub深度集成
- 支持NPM、Docker等多种包
- 按存储库设置权限
Verdaccio详细配置
基本配置
Verdaccio的配置文件通常位于~/.config/verdaccio/config.yaml
,主要配置项包括:
storage: ./storage # 包存储路径
plugins: ./plugins # 插件目录
web:
title: Verdaccio # Web界面标题
# comment out to disable gravatar support
# gravatar: false
auth:
htpasswd:
file: ./htpasswd # 用户认证文件
# 最大用户数,默认1000
# max_users: 1000
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
'@*/*':
access: $authenticated
publish: $authenticated
proxy: npmjs
'**':
access: $authenticated
publish: $authenticated
proxy: npmjs
server:
keepAliveTimeout: 60
middlewares:
audit:
enabled: true
logs:
- {type: stdout, format: pretty, level: http}
#- {type: file, path: verdaccio.log, level: info}
用户认证
创建用户:
npm adduser --registry http://localhost:4873
或者使用htpasswd工具:
htpasswd -c /path/to/htpasswd username
发布包到私有仓库
首先配置项目使用私有仓库:
npm config set registry http://localhost:4873
然后发布包:
npm publish
Nexus配置NPM仓库
安装与基本配置
- 下载并安装Nexus Repository Manager
- 启动后访问http://localhost:8081
- 创建NPM仓库:
- 创建hosted类型仓库用于私有包
- 创建proxy类型仓库代理npmjs.org
- 创建group类型仓库组合上述仓库
权限配置
- 创建NPM角色
- 分配相应权限:
- nx-repository-view-npm-*-read
- nx-repository-view-npm-*-write
- 创建用户并分配角色
客户端配置
配置npm使用Nexus仓库:
npm config set registry http://localhost:8081/repository/npm-group/
认证配置:
npm login --registry=http://localhost:8081/repository/npm-hosted/
企业级最佳实践
多环境仓库策略
建议设置以下仓库:
- npm-release: 发布稳定版本
- npm-snapshot: 发布测试版本
- npm-proxy: 代理公共仓库
- npm-group: 组合上述仓库
CI/CD集成
在CI流程中配置:
steps:
- name: Install dependencies
run: |
npm config set registry $NPM_REGISTRY
echo "//nexus.example.com/repository/npm-group/:_authToken=$NPM_TOKEN" > .npmrc
npm install
- name: Publish package
if: github.ref == 'refs/heads/main'
run: |
npm config set registry $NPM_RELEASE_REGISTRY
npm publish
监控与维护
- 定期清理旧版本的包
- 监控存储空间使用情况
- 设置包保留策略
- 定期备份关键数据
常见问题解决
认证问题
如果遇到认证错误,检查以下内容:
- 确保.npmrc文件包含正确的认证信息:
registry=http://localhost:4873 //localhost:4873/:_authToken="your_token"
- 确认用户有正确的权限
- 检查服务端日志获取详细错误
代理配置
如果需要通过代理访问:
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
或者在.npmrc中配置:
proxy=http://proxy.company.com:8080
https-proxy=http://proxy.company.com:8080
strict-ssl=false
性能优化
对于大型团队:
- 配置上游缓存
- 增加存储后端性能
- 考虑分布式部署
- 启用CDN加速
高级主题
作用域包管理
使用作用域可以更好地组织私有包:
{
"name": "@company/utils",
"version": "1.0.0"
}
配置作用域包使用私有仓库:
npm config set @company:registry http://localhost:4873
自定义插件开发
Verdaccio支持自定义插件,例如开发认证插件:
module.exports = (config, stuff) => {
return {
authenticate(user, password, cb) {
if (user === 'admin' && password === 'secret') {
return cb(null, {name: 'admin'});
}
cb(new Error('认证失败'));
}
};
};
然后在配置中启用:
auth:
custom-auth:
plugin: './path/to/plugin'
迁移策略
从公共仓库迁移私有包:
- 在私有仓库中创建相同的包名
- 下载公共包:
npm pack package-name
- 解压并修改package.json
- 发布到私有仓库
安全考虑
访问控制
- 限制匿名访问
- 实施最小权限原则
- 定期审查用户权限
- 启用双因素认证
依赖安全
- 集成漏洞扫描工具
- 设置自动安全更新
- 审查第三方依赖
- 维护允许的包列表
审计日志
确保记录以下操作:
- 包发布
- 包删除
- 用户登录
- 权限变更
与其他工具集成
与Lerna配合使用
在lerna.json中配置:
{
"command": {
"publish": {
"registry": "http://localhost:4873"
}
}
}
与Yarn配合使用
配置.yarnrc:
registry "http://localhost:4873"
与PNPM配合使用
配置.npmrc:
registry=http://localhost:4873
上一篇: NPM依赖管理
下一篇: 常用NPM替代工具(yarn/pnpm)