架设私有的npm包管理器.md
架设私有的npm包管理器
常用搭建工具
工具名称 | 说明 |
---|---|
nexus | nexus功能强大,但是部署和配置比较繁琐 |
cnpm | cnpm虽然比nexus配置简单,但是需要用到MySQL |
verdaccio | verdaccio使用文件存储包数据,无需安装数据库并且部署简单,只需一行代码就可以部署完成 |
sinopia | sinopia已经没有维护了 |
Verdaccio 搭建私有 NPM
Verdaccio 是一个 Node.js创建的轻量的私有npm proxy registry
特点
- 它是基于Node.js的网页应用程序
- 它是私有npm registry
- 它是本地网络proxy
- 它是可插入式应用程序
- 它相当容易安装和使用
- 提供Docker和Kubernetes支持
- 它与yarn, npm 和pnpm 100% 兼容
- 它forked于sinopia@1.4.0并且100%** 向后兼容**
要求
-
需要Node.js
v12
或更高版本 -
Node包管理器
npm
、pnpm
或yarn
(classic以及berry)。强烈建议使用最新版本的Node包管理器,比如
> npm@6.x | yarn@1.x || yarn@2.x | pnpm@6.x
。不再支持npm@5.x
或更低版本。 -
使用现代浏览器访问Web界面,支持使用
Chrome、Firefox、 Edge、 IE11
浏览器。Verdaccio将根据 Node.js发布工作组 的推荐支持最新的 Node.js 版本
安装
1. 安装Node
Node.js是一个基于 Chrome V8 引擎的 JavaScript 运行环境,这样JavaScript 的运行可以不用依赖浏览器。
配置node 环境变量
- 查看nodejs全局安装路径:npm config ls其中 prefix 的路径就是 nodejs全局安装路径。
- 将 nodejs全局安装路径 配置到环境变量中即可。
2. verdaccio安装
在命令行中执行:
npm install -g verdaccio
3. 运行verdaccion
verdaccio
4. 作为 Windows 服务安装
- 为 verdaccio创建一个目录
mkdir c:\verdaccio
cd c:\verdaccio
- 本地安装verdaccio
- npm install verdaccio
- 在此位置(c:\verdaccio\config.yaml)创建
config.yaml
文件 - Windows服务设置
使用NSSM
- 下载NSSM并解压
- 设置环境变量,将包含 nssm.exe 的路径到PATH(路径)中
- 打开管理命令
- 运行
nssm install verdaccio
,设置Application选项卡中的数据- Path:
node
- Startup diretory:
c:\verdaccio
- Arguments:
c:\verdaccio\node_modules\verdaccio\build\lib\cli.js -c c:\verdaccio\config.yaml
- Path:
- 执行
nssm start verdaccio
启动verdaccio
服务
5. verdaccion配置
# # This is the default config file. It allows all users to do anything, # so don't use it on production systems. # # Look here for more config file examples: # https://github.com/verdaccio/verdaccio/tree/master/conf # # path to a directory with all packages storage: ./storage # path to a directory with plugins to include plugins: ./plugins web: title: Verdaccio # comment out to disable gravatar support # gravatar: false # by default packages are ordercer ascendant (asc|desc) # sort_packages: asc # convert your UI to the dark side # darkMode: true # logo: http://somedomain/somelogo.png # favicon: http://somedomain/favicon.ico | /path/favicon.ico # rateLimit: # windowMs: 1000 # max: 10000 # translate your registry, api i18n not available yet # i18n: # list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations # web: en-US auth: htpasswd: file: ./htpasswd # Maximum amount of users allowed to register, defaults to "+inf". # You can set this to -1 to disable registration. # max_users: 1000 # a list of other known repositories we can talk to uplinks: npmjs: url: https://registry.npm.taobao.org packages: '@*/*': # scoped packages access: $all publish: $authenticated unpublish: $authenticated proxy: npmjs '**': # allow all users (including non-authenticated users) to read and # publish all packages # # you can specify usernames/groupnames (depending on your auth plugin) # and three keywords: "$all", "$anonymous", "$authenticated" access: $all # allow all known users to publish/publish packages # (anyone can register by default, remember?) publish: $authenticated unpublish: $authenticated # if package is not available locally, proxy requests to 'npmjs' registry proxy: npmjs # You can specify HTTP/1.1 server keep alive timeout in seconds for incoming connections. # A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout. # WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enough. server: keepAliveTimeout: 60 middlewares: audit: enabled: true # log settings logs: { type: stdout, format: pretty, level: http } #experiments: # # support for npm token command # token: false # # disable writing body size to logs, read more on ticket 1912 # bytesin_off: false # # enable tarball URL redirect for hosting tarball with a different server, the tarball_url_redirect can be a template string # tarball_url_redirect: 'https://mycdn.com/verdaccio/${packageName}/${filename}' # # the tarball_url_redirect can be a function, takes packageName and filename and returns the url, when working with a js configuration file # tarball_url_redirect(packageName, filename) { # const signedUrl = // generate a signed url # return signedUrl; # } # This affect the web and api (not developed yet) i18n: web: zh-CN listen: '0.0.0.0:4873' # - localhost:4873 # default value # - http://localhost:4873 # same thing # - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY) # - https://example.org:4873 # if you want to use https # - "[::1]:4873" # ipv6 # - unix:/tmp/verdaccio.sock # unix socket
6. 常用命令
添加用户
npm adduser --registry http://服务器IP地址:4873
发布包
npm publish --registry http://服务器IP地址:4873/
注意:在发布包之前需要修改
package.json
文件,将"private"配置改为false
发布包排除文件,新建.npmignore
文件,配置发布包排除文件:
# 打包忽略文件 .DS_Store node_modules public /dist /src/assets/logo.png /src/test /src/main.ts # local env files .env.local .env.*.local # Log files npm-debug.log* yarn-debug.log* yarn-error.log* pnpm-debug.log* # Editor directories and files .idea .vscode *.suo *.ntvs* *.njsproj *.sln *.sw? .browserslistrc .eslintrc.js babel.config.js publish.bat tsconfig.json
安装包
npm install 包名称 --registry http://服务器IP地址:4873
更新包
npm update 包名称 --registry http://服务器IP地址:4873
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异