侧边栏壁纸
博主头像
Angel博主等级

行动起来,活在当下

  • 累计撰写 20 篇文章
  • 累计创建 8 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

搭建私有化npm包资源库

Angel
2023-10-08 / 0 评论 / 0 点赞 / 63 阅读 / 4087 字
温馨提示:
本文最后更新于 2023-12-19,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

搭建私有npm包资源库

工具

Verdaccio 是一个 Node.js创建的轻量的私有npm proxy registry(包的资源库)

1. npm下载

npm install -g verdaccio

2. 修改配置

storage: ../storage
plugins: ../plugins
web:
  title: Verdaccio
i18n:
  web: zh-CN
auth:
  htpasswd:
    file: ./htpasswd
    max-users: -1
uplinks:
  npmjs:
    url: https://registry.npmmirror.com
packages:
  '@liuxiaopeng/*':
    access: $authenticated
    publish: $authenticated
    unpublish: admin userA
  '@*/*':
    access: $authenticated
    publish: $authenticated
    unpublish: $authenticated 
    proxy: npmjs
  '**':
    access: $authenticated
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs
server:
  keepAliveTimeout: 60
  VERDACCIO_PUBLIC_URL: 'https://npm.liuxiaopeng.cn'
middlewares:
  audit:
    enabled: true
search: true
log: { type: stdout, format: pretty, level: http }
listen:
  - 0.0.0.0:4873

3. 运行

verdaccio
# pm2运行
pm2 start verdaccio

4. https 方式访问

默认是本地运行 反向代理nginx 代理4873端口 (注意防火墙端口开放 4873) 配置如下

location ^~ / {
    proxy_pass http://127.0.0.1:4873; 
    proxy_set_header Host            $host:$server_port; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header REMOTE-HOST $remote_addr; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "upgrade"; 
    proxy_http_version 1.1; 
    add_header X-Cache $upstream_cache_status; 
    # 重要
    proxy_set_header X-Forwarded-Proto $scheme;
}

5. 添加npm用户

npm adduser --registry http://npm.liuxiaopeng.cn/
错误分析
# 容器报错:
 Error: EACCES: permission denied, open '/verdaccio/storage/htpasswd'
 http --- 500, user: null(36.44.164.169 via 10.0.8.9), req: 'PUT /-/user/org.couchdb.user:seazt', error: internal server error
 http --- 10.0.8.9 requested 'PUT /-/user/org.couchdb.user:seazt'
 error--- the user seazt could not being added. Error: EACCES: permission denied, open '/verdaccio/storage/htpasswd'
 error--- unexpected error: EACCES: permission denied, open '/verdaccio/storage/htpasswd'
# 终端报错
 500 Internal Server Error - PUT https://npm.seazt.site/-/user/org.couchdb.us

检查用户配置映射的文件 一般是 /verdaccio/storage/htpasswd 文件的写入权限问题

6. 添加私有包资源库地址

nrm ls //展示所有的通用资源,
nrm add myNpm https://npm.liuxiaopeng.cn/
nrm use myNpm //别名
npm i package //就能下载你服务上的package,要切换回去,可以nrm use npm 或其他资源。

7. 登录

确认包资料库地址是不是搭建的私有化地址

npm login

8. 发布包

npm publish --registry https://npm.liuxiaopeng.cn/
0

评论区