本文深入探讨如何设置私有 npm 仓库,涵盖多种方案及实用代码示例,助您快速上手。无论您是个人开发者还是大型团队成员,私有化托管 npm 包都能提升安全性、灵活性及控制力。
为何选择私有 npm 仓库?
私有 npm 仓库托管方案
自托管方案
Git 托管平台
npm 企业版
1. 使用 verdaccio 设置私有 npm 注册表
verdaccio 是易于配置和使用的开源 npm 注册表代理,支持托管私有包并缓存公共包。
1.1 安装 verdaccio (假设已安装 Node.js)
npm install --global verdaccio
1.2 启动 verdaccio
verdaccio
verdaccio 默认端口为 4873。访问 http://localhost:4873 查看其 UI。
1.3 配置 verdaccio
verdaccio 首次运行时会创建默认配置文件 (路径可能因系统而异,例如 ~/.config/verdaccio/config.yaml),可自定义配置:
storage: ./storage auth: htpasswd: file: ./htpasswd max_users: 100 uplinks: npmjs: url: https://registry.npmjs.org/ packages: '@*/*': access: $all publish: $authenticated proxy: npmjs '**': access: $all publish: $authenticated proxy: npmjs middlewares: audit: enabled: true logs: - { type: stdout, format: pretty, level: http }
1.4 创建用户并登录
npm adduser --registry http://localhost:4873
系统将提示您输入用户名、密码和邮箱。
1.5 发布包
在包含有效 package.json 的包目录下:
npm publish --registry http://localhost:4873
1.6 从私有注册表安装
npm install <package-name> --registry http://localhost:4873
registry=http://localhost:4873
2. 使用 GitHub Packages (略,与原文类似,只是调整措辞)
3. 使用 GitLab Packages (略,与原文类似,只是调整措辞)
4. 使用 Sonatype Nexus 或 JFrog Artifactory 自托管 (略,与原文类似,只是调整措辞)
5. npm 企业版 (略,与原文类似,只是调整措辞)
最佳实践与技巧
总结
设置私有 npm 仓库能安全地管理和托管您的包。 选择合适的方案取决于您的组织对安全性、可扩展性和维护性的需求。 希望本文提供的示例和代码片段能帮助您开始托管自己的私有 npm 包。
以上就是私有 npm 存储库的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号