使用Verdaccio搭建npm私有仓库

上周淘宝源https://registry.npm.taobao.org/证书过期,导致我们的项目部署不了。只能紧急的将上线项目的源改为cnpm的。现在一周时间过去了,淘宝源的证书还没更新,我们还有其他项目准备上线。

我们不清楚淘宝源什么时候会更新证书,也不清楚cnpm什么时候出现不可预测的问题。所以我们不能将希望都寄托在第三方身上,决定搭建自己的npm私有仓库。

经过调研(就百度了一下),我们决定使用Verdaccio搭建自己的npm私有仓库。

下面使用docker搭建Verdaccio

创建目录

verdaccio暴露了三个目录confstorageplugins提供映射,可以将容器内部的文件夹映射至宿主机,这里把目录映射出来,这样在容器内部出现问题,其文件还能保留下来,重新制作容器、挂载目录即可恢复,同时修改一些配置也会比较方便,我这里将目录挂在~/verdaccio下。

mkdir ~/verdaccio
# 配置文件目录
mkdir ~/verdaccio/conf
# 发布的包存储目录
mkdir ~/verdaccio/storage
# 插件目录
mkdir ~/verdaccio/plugins

# docker容器中使用的用户id10001 运行的程序,所以要对目录进行授权,否则容器启动失败(读取不了配置文件)
chown 10001:65535 ~/verdaccio -R

创建配置文件config.yaml

cd ~/verdaccio/conf
vim config.yaml

配置文件

#
# This is the config file used for the docker images.
# It allows all users to do anything, so don't use it on production systems.
#
# Do not configure host and port under `listen` in this file
# as it will be ignored when using docker.
# see https://github.com/verdaccio/verdaccio/blob/master/wiki/docker.md#docker-and-custom-port-configuration
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# 发布的包存储路径
storage: /verdaccio/storage

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
security:
  api:
    jwt:
      sign:
        expiresIn: 60d
        notBefore: 1
  web:
    sign:
      expiresIn: 7d

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://r.cnpmjs.org/

packages:
  '@jota/*':
      access: $all
      publish: $all

  '@*/*':
    # scoped packages
    access: $all
    publish: $all
    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 packages
    # (anyone can register by default, remember?)
    publish: $all

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true

# log settings
logs:
  - {type: stdout, format: pretty, level: trace}
  #- {type: file, path: verdaccio.log, level: info}

docker-compose配置

创建docker-compose配置

vim ~/verdaccio/docker-compose.yml
version: '3'

services:
  verdaccio:
    image: verdaccio/verdaccio:4
    container_name: verdaccio
    restart: always
    ports:
      - 4873:4873
    volumes:
      - ./storage:/verdaccio/storage
      - ./conf:/verdaccio/conf
      - ./plugins:/verdaccio/plugins
      - ./logs:/verdaccio/logs
    environment:
      NODE_ENV: production
      VERDACCIO_PROTOCOL: http
      VERDACCIO_TITLE: Verdaccio
      VERDACCIO_LOG_LEVEL: http
      VERDACCIO_ALLOW_OFFLINE: true
      VERDACCIO_API_SECRET: mysecret
      VERDACCIO_WEB_SECRET: mysecret
      VERDACCIO_AUTH_TOKEN_EXPIRES: 60d
      VERDACCIO_WEB_TOKEN_EXPIRES: 7d
      VERDACCIO_ISSUER: verdaccio
      VERDACCIO_AUDIENCE: verdaccio
      VERDACCIO_UPLINKS_NPMJS_URL: https://r.cnpmjs.org/
      VERDACCIO_UPLINKS_NPMJS_CACHE: true
      VERDACCIO_UPLINKS_NPMJS_MAXAGE: 300000
      VERDACCIO_UPLINKS_NPMJS_MAXFAIL: 2
      VERDACCIO_UPLINKS_NPMJS_FAIL_TIMEOUT: 10000
      VERDACCIO_UPLINKS_NPMJS_MAX_OFFLINE_TIME: 300000
      VERDACCIO_UPLINKS_NPMJS_SCOPED_PACKAGES: true
      VERDACCIO_UPLINKS_NPMJS_FETCH_METADATA: true
      VERDACCIO_UPLINKS_NPMJS_FETCH_PACKAGE: true
      VERDACCIO_UPLINKS_NPMJS_CONCURRENCY: 4
      VERDACCIO_UPLINKS_NPMJS_MAX_PAYLOAD_SIZE: 1024
      VERDACCIO_UPLINKS_NPMJS_MAX_SOCKETS: 100
      VERDACCIO_UPLINKS_NPMJS_TIMEOUT: 10000
      VERDACCIO_UPLINKS_NPMJS_MAX_RETRY: 3

测试

访问http://192.168.56.108:4873/
在这里插入图片描述

更改源

  • 下载源管理工具

    npm i yrm -g
    
  • 添加源

    yrm add verdaccio http://192.168.56.108:4873
    
  • 使用源

    yrm use verdaccio
    
  • 31
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值