使用 verdaccio 搭建 npm 服务器

2019-06-26  本文已影响0人  kruz

有时候我们想用公司的npm包,而某些代码又是私密的,就需要部署到npm私有服务器。verdaccio是一个私有的npm包管理工具,安装方便。

安装

npm install –g verdaccio

运行

安装完成后直接输入 verdaccio 命令即可启动,第一次启动时会创建配置文件~/.config/verdaccio/config.yaml
默认只能在内网访问:
我们在配置文件最后一行加上, 然后启动 verdaccio

storage: ./storage  //npm包存放的路径

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: -1  //默认为1000,改为-1,禁止注册

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: http://registry.npm.taobao.org/  //默认为npm的官网,由于国情,改为淘宝源
    
packages:  //配置权限管理
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated

  '*':
    # 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: npmjs  // 若安装的报在simopia没有,会去配置的npmjs地址去下载

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

# storage: ./storage  //npm包存放的路径

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: -1  //默认为1000,改为-1,禁止注册

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: http://registry.npm.taobao.org/  //默认为npm的官网,由于国情,改为淘宝源
    
packages:  //配置权限管理
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated

  '*':
    # 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: npmjs  // 若安装的报在simopia没有,会去配置的npmjs地址去下载

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

# 默认没有,只能在本机访问,添加后可以通过外网访问。
listen: 0.0.0.0:4873  // 默认没有,只能在本机访问,添加后可以通过外网访问。

如何使用

打开浏览器输入对应的暴露的地址,比如192.168.0.1:4873

截图如下:

预览

按照截图提示,注册一个用户

npm adduser --registry http://192.168.0.1:4873

更改下载源

在客户端新建一个.npmrc文件, 写入对应的地址:

registry=http://192.168.0.1:4873

发布一个包

如果需要发布一个包,需要先进行登录。

npm login

登录成功之后,在对应的包文件夹根目录,增加package.json版本号,再执行发布命令即可。

npm publish

其他

verdaccio会帮我们缓存下载的包,后面再安装已缓存的依赖时速度回很快。

上一篇 下一篇

猜你喜欢

热点阅读