搭建私有 npm 仓库 verdaccio
发布于 3 年前 作者 18820227745 3663 次浏览 来自 分享

搭建私有 npm 仓库:verdaccio

背景

  • npm 私有仓库的原理
    • 用户 install 后向私有 npm 发起请求,服务器会先查询所请求的这个模块是否是我们自己的私有模块或已经缓存过的公共模块,如果是则直接返回给用户;如果请求的是一个还没有被缓存的公共模块,那么则会向上游源请求模块并进行缓存后返回给用户。上游的源可以是 npm 仓库,也可以是淘宝镜像。
  • 为什么要搭建私有 npm 仓库?
    • 1、安全性角度考虑:如果我们想要一个公共组件库,那么把组件放到我们私有库中,只有内网可以访问,这样可以避免组件中业务的泄露;
    • 2、模块复用性角度考虑:多个项目之间有重复的共有模块,当需要修改模块,通过简单的统一的配置就可以实现;提炼后的组件有专门的地址可以用来查看,方便使用,在后期项目的引用中也能节约开发成本;
    • 3、npm 包下载速度角度考虑:使用内部的地址,能够在开发下载 node 包的同时,将关联的依赖包缓存到 npm 私有仓库服务器中,下载速度更快;
    • 4、项目开发中的路劲角度考虑:在项目开发中书写代码更整洁简练,不需书写更长的相对路径;
    • 5、公司技术沉淀角度考虑:知识的沉淀,在公司业务相关的应用上尤佳;
    • 6、版本角度的考虑:相当于一个容器,统一管理需要的包,保持版本的唯一;
    • 7、开发效率角度考虑:使私有公共业务或组件模块能以共有包一样的管理组织方式,保持一致性,提高开发效率.
  • 搭建私有 npm 仓库的要求?
    • 存储要求:私有的 npm 包要 100 %安全,要有备份机制、恢复机制
      • 解决:私有的 npm 包存储到 oss 上,利用 oss 备份机制来备份、恢复
    • 安全性要求:只允许公司内部访问,要有账号才能访问
      • 解决:私有 npm 网站域名只允许内部访问,关闭注册功能,只允许内部账号访问
    • 规范要求:不允许随意发布不符合命名规则的私有 npm 包
      • 解决:使用内部账号才能发布私有的 npm 包,私有 npm 包必须是 @公司名/**** 格式才能发布成功

部署使用

本地测试:

  • docker 启动
docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio

Dockerfile 启动

  • Dockerfile

    FROM node:lts-alpine as builder
    # 安装插件
    RUN mkdir -p /verdaccio/plugins \
     && cd /verdaccio/plugins \
     && npm install --global-style --no-bin-links --omit=optional verdaccio-auth-memory@latest verdaccio-aliyunoss-storage
    FROM verdaccio/verdaccio:5
    USER root
    # 复制配置文件
    ADD config/config.yaml /verdaccio/conf/config.yaml 
    ADD config/htpasswd /verdaccio/conf/htpasswd
    # 拷贝插件文件
    COPY --chown=$VERDACCIO_USER_UID:root --from=builder \
     /verdaccio/plugins/node_modules/verdaccio-auth-memory \
     /verdaccio/plugins/verdaccio-auth-memory
    COPY --chown=$VERDACCIO_USER_UID:root --from=builder \
     /verdaccio/plugins/node_modules/verdaccio-aliyunoss-storage \
     /verdaccio/plugins/verdaccio-aliyunoss-storage
    # 配置文件权限调整
    RUN chown 10001:65533 /verdaccio/conf/htpasswd
    EXPOSE 4873
    
    cd /verdaccio
    docker build -t verdaccio:v1 . 
    
    • 启动 Docker
    docker run -it --rm --name verdaccio -p 4873:4873 verdaccio:v1
    

    配置

    • 添加阿里云 oss 存储
      • 线上 endpoint 可以配制成内网访问,加速访问,节约费用,如oss-cn-shanghai-internal.aliyuncs.com
    # config/config.yaml
    store:
     aliyunoss-storage:
     bucket: xxx
     keyPrefix: npm/packages
     # https://help.aliyun.com/document_detail/31837.html
     endpoint: xxx.aliyuncs.com
     accessKeyId: xxx
     secretAccessKey: xxx
    
    • 登录配置

      • 添加用户测试:test 123456
      • 配置成允许注册
      # config/config.yaml
      auth:
       htpasswd:
       file: /verdaccio/conf/htpasswd 
       # Maximum amount of users allowed to register, defaults to "+inf".
       # You can set this to -1 to disable registration.
       max_users: 1000
      
      • 添加用户
      npm adduser --registry http://0.0.0.0:4873/
      
      • 获取用户加密信息
      docker ps -a | grep verdaccio 
      docker exec -it 6d2c7ab0200d sh
      /opt/verdaccio # cat /verdaccio/conf/htpasswd
      
      • 复制到 config/htpasswd 文件中
      test:ITVpjPJu6yRDA:autocreated 2022年08月30日T07:21:56.542Z
      
      • 关闭注册功能
      # config/config.yaml
      auth:
       htpasswd:
       file: /verdaccio/conf/htpasswd 
       # Maximum amount of users allowed to register, defaults to "+inf".
       # You can set this to -1 to disable registration.
       max_users: -1
      
      • 再次启动测试
        • 使用 test 可以登录
        • 注册账号:test1 失败

发布私有 npm 包

  • 配置:
    • 登录的用户才可以查看和发布 npm 包
    • 发布的 npm 必须以 @公司名/* 规范命名
# config/config.yaml
packages:
 # '@jota/*':
 # access: $all
 # publish: $all
 # '@*/*':
 # # scoped packages
 # access: $all
 # publish: $all
 # proxy: npmjs
 '**':
 access: $all
 publish: $authenticated
 proxy: npmjs
 # 登录的用户才可以查看和发布 npm 包
 # 发布的 npm 必须以 @公司名/* 规范命名
 '@公司名/*':
 access: $authenticated
 publish: $authenticated
  • 发布 npm 包
    • 私有 npm 注意点:
      • 包名规范:package.json name 规范:@公司名/xxx
      • 上传打包后的文件,不上传 src 源码:
    • CD 到私有 npm 项目根目录
    • login: npm adduser --registry @公司名
    • publish:npm publish --registry http://0.0.0.0:4873/

使用私有 npm 包

  • 获取 _authToken:
    • 登录:npm adduser --registry http://0.0.0.0:4873/
    • 查看 _authToken: cat ~/.npmrc
  • 项目中安装

    • 项目文件夹下添加 .npmrc 文件
    registry=http://0.0.0.0:4873/
    //0.0.0.0:4873/:_authToken=****
    always-auth=true
    
    • Dockerfile 中添加
    RUN npm config set registry http://0.0.0.0:4873/ && \
    npm config set @公司名:registry http://0.0.0.0:4873/ && \
    npm config set //0.0.0.0:4873/:_authToken ****
    

参考

回到顶部

AltStyle によって変換されたページ (->オリジナル) /