Skip to content

搭建私有 Npm 仓库

约 883 字大约 3 分钟

npmprivate

2024-08-23

背景

  1. 之前公司有自己的 npm 私有仓库,但是因为阿里云提供制品仓库服务,就废弃了自己的私服
  2. 云效仓库不稳定,流水线莫名出现第三方包找不到的问题,阿里云长时间未解决
  3. 云效仓库突然对 yarn 的支持不友好,使用 yarn 装包频繁出现打包失败的问题,个别流水线必现
  4. 目前的 npm 包缺乏包相关的使用文档

技术选型

常见的私服方案一般有三种:

最终选择了Verdaccio方案,原因如下:

  1. 之前的私服就是使用 Verdaccio 搭建的,部分配置可以快速复用
  2. Verdaccio 是所有的私服方案中,在易用性和搭建复杂度上平衡的最好的

私服搭建

以下是旧私服信息

进入项目后,发现项目可以使用,只是Verdaccio版本较老(Version 4),一些功能存在缺失。另外 Verdaccio 指令并不存在于 npm bin 目录下,而是存在于 usr/bin 下。将usr/bin/ 下的指令删除后,使用 npm 对 Verdaccio 版本进行升级(Version 5)。

升级后的目录结构:

    ├── conf/config.yaml   Verdacccio配置文件
    ├── npmServer.sh          Verdaccio 启动脚本
    ├── htpasswd                  存放密码文件
    ├── storage                      包存放文件夹
    ├── verdaccio.log            日志文件(只记录error级别)

具体相关配置,有兴趣的请自行查看config.yaml

私服使用

开发者

安装nrm,执行

nrm add ncNpm http://127.0.0.1:4876/
nrm use ncNpm

按照提示,输入用户名、密码和邮箱,之后可以使用私服的所有功能

流水线

将流水线的包安装命令改为

yarn  --registry=http://127.0.0.1:4876

或在安装包前添加

yarn config set registry http://127.0.0.1:4876
npm config set registry=http://127.0.0.1:4876
npm config set always-auth true

解决不同步问题

目前的修复流程:

  1. 登录d8
  2. 修改/data/ncnpm/conf/config.yaml
  3. 注释掉76行,放开77行
  4. 执行 pm2 restart npmServer重启npm服务
  5. 在本地随便找个临时目录,执行 yarn add 报名@版本号,强制同步
  6. 第3步取反(否则源可能有问题),然后再执行第4步
#
# 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
web:
    enable: true
    title: NPM仓库
    i18n: zh-CN
    logo: https://static.nowcoder.com/fe/file/logo/1.png
    #logo: https://ncstatic.oss-cn-hangzhou.aliyuncs.com/fe/file/logo/1.png
    primary_color: "#32ca99"
    favicon: https://static.nowcoder.com/images/logo_87_87.png
    #favicon: https://ncstatic.oss-cn-hangzhou.aliyuncs.com/images/logo_87_87.png
storage: ../storage

#listen: https://d8.nowcoder.com:4876/

#https:
#key: /home/web/.config/verdaccio/verdaccio-key.pem
#    cert: /home/web/.config/verdaccio/verdaccio-cert.pem
#    ca: /home/web/.config/verdaccio/verdaccio-csr.pem
listen: 0.0.0.0:4876

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:
  alinpm:
    url: https://packages.aliyun.com/6128d2e757e7cd986dfae5ab/npm/npm-registry/
    cache: true
    auth:
      type: bearer
      token: "a010fc30-6ea6-4073-9d24-7e1dbab8d2a3"
  taobaonpm:
    url: https://registry.npmmirror.com/
  txnpm:
    url: http://mirrors.cloud.tencent.com/npm/
  huaweinpm:
    url: http://mirrors.huaweicloud.com/repository/npm/

packages:
  '@fe/*':
    # scoped packages
    access: $all
    publish: undefined
    proxy: alinpm
  '@nc/*':
    # scoped packages
    access: $all
    publish: undefined
    proxy: alinpm
  '@ncfe/*':
    #scoped packages
    access: $all
    publish: undefined
    proxy: alinpm
  '@ncwp/*':
    #scoped packages
    access: $all
    publish: undefined
    proxy: alinpm
  '@babel/*':
    #scoped packages
    access: $all
    publish: undefined
    proxy: alinpm
    # proxy: alinpm taobaonpm txnpm huaweinpm
  '**':
    # 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 packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: alinpm
    # proxy: alinpm taobaonpm txnpm huaweinpm

# log settings
log: {type: file, format: pretty-timestamped, path: './verdaccio.log', level: error}

TODO

  1. 私服的容灾备份
  2. 接入webhook
  3. 增加内网访问权限