npm私有仓库搭建及使用 (verdaccio)

# 下载及搭建

1
2
3
4
$ npm install -g verdaccio
$ verdaccio
  warn --- config file  - /home/ubuntu/.config/verdaccio/config.yaml
  warn --- http address - http://localhost:4873/ - verdaccio/2.3.2

终端显示默认配置文件和verdaccio工作端口, 浏览器打开http://localhost:4873/ ,页面如下 IMAGE

# 配置

default config:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  
  storage: ./storage      # 设置托管或缓存包的存放目录
  auth:                   # 权限控制
    htpasswd:             # 启用 htpasswd 插件管理权限
      file: ./htpasswd    # 制定 htpasswd 文件路径,htpasswd 中存储者用户名和加密过的秘钥
      max_users: 1000     # 最多允许注册用户数
  uplinks:                # 设置外部仓储,如果 verdaccio 找不到请求的包(非 verdaccio 托管),就会查找外部仓储
    npmjs:
      url: https://registry.npmjs.org/
  packages:
    '@*/*':
      access: $all
      publish: $authenticated
      proxy: npmjs
    '**':
      proxy: npmjs
  logs:
    - {type: stdout, format: pretty, level: http}
  web:
    enable: true
    title: Verdaccio
    logo: logo.png
  publish:
    allow_offline: false    # 是否支持离线发布 默认false
  url_prefix: https://dev.company.local/verdaccio/ 
  # 设置资源文件路径前缀。默认不需要设置,但如果使用 nginx 代理并改写了请求路径,就需要指定了。
  max_body_size: 1mb        # 默认JSON document size 1mb
  listen:                   # 设置服务运行地址端口
  # - 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
  
  • uplinks常用仓储有

    1
    2
    3
    4
    5
    6
    
      npmjs:
        url: https://registry.npmjs.org
      yarnjs:
        url: https://registry.yarnpkg.com
      cnpmjs:
        url: https://registry.npm.taobao.org
    
  • packages: 包访问或发布控制

    • {regexp}: 包名匹配正则。 access: 访问控制,可选值有$all(用户不限制), $anonymous(用户不限制), $authenticated(所有登录用户), username( 用户名,需指定具体用户,可指定多个用户,用户间空格隔开,如 secret super-secret-area ultra-secret-area)。尽管@all, @anonymous, all, undefined, publish: 发布控制,配置请参考 access proxy: 代理控制,设置的值必选现在 uplinks 中定义。

    • 常用的包名正则有:

      1
      2
      3
      4
      
      **         # 匹配任意包
      @*/*       # 匹配任意 scope 包
      @npmuser/* # 匹配 scope 为 npmuser 的包
      npmuser-*  # 匹配包名有 npmuser- 前缀的包
      

      包名正则规范通 gitignore 一致,verdaccio 内部使用minimatch实现的,如果需要书写更复杂的正则,可以参考 minimatch 文档。

详情(https://github.com/verdaccio/verdaccio/blob/master/wiki/config.md)

# 使用

# 项目初始化

1
2
$ yarn/npm init
... input some info

# coding

# 注册仓库并添加用户

1
2
3
npm set registry ip/hostname
npm adduser --registry ip/hostname
input: username/password/email

# publish

1
2
$ yarn/npm pulish
... input some info

# install

1
2
3
4
$ yarn add package-name / npm install package-name
or
$ yarn / npm logout
$ (yarn add package-name / npm install package-name) --registry http://localhost:4873

# 私有包

现在配置org-前缀的包全部私有

只需在配置文件 config.yml 中 package 段添加配置

1
2
3
4
  'org-*':
    access: $authenticated
    publish: $authenticated
    proxy: npmjs

这里我们配置了所有org-前缀的包只有注册用户才能访问和发布。

你也可以对 publish 做进一步限制,只有 npmuser 用户才能发布

1
2
3
4
  'org-*':
    access: $authenticated
    publish: npmuser
    proxy: npmjs

注意修改配置后要重启 verdaccio

# scope 包

其实加前缀并不是一种很好组织包的方式,npm 提供了更好的名称空间策略 scope

scope 包包名格式:@scope-name/pkg-name

初始化包时指定 scope

1
  npm init --scope org

我们可以为 scope 绑定一个仓储

1
2
  npm login --registry=http://reg.example.com --scope=@org
  npm config set @org:registry http://reg.example.com

这样凡是碰到 scope 为 @org 的包,npm 将自动切换作业仓储到 scope 绑定的仓储,这提供了一种多仓储策略。

scope 私有包配置

1
2
3
4
  '@org/*':
    access: $authenticated
    publish: $authenticated
    proxy: npmjs
Licensed under CC BY-NC-SA 4.0