avatar

使用 verdaccio 搭建私有npm镜像

使用 verdaccio 搭建私有npm镜像

背景

  1. 私有包,统一管理,方便开发和使用,自然也可以使用npm的付费服务,可惜贫穷限制了消费能力
  2. 安全性,内部开发的模块和一些内容并不希望其他无关人员能够看到,但是又希望内部能方便使用
  3. 加速,自己搭建npm 服务器,本身可以自带常用package的缓存, cnpm 有一些包存在路径问题,而npm 的速度有些感人,自建的服务器会缓存下载过的包,能节省时间

部署流程

安装相关包

1
2
3
npm i -g verdaccio
npm i -g pm2
npm i -g nrm

配置 verdaccio

直接运行 verdaccio, 控制台会有提示配置文件的路径,修改一下配置文件vim xxx/config.yaml

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# path to a directory with all packages
storage: ./storage
# path to a directory with plugins to include
plugins: ./plugins

web:
title: Verdaccio
# comment out to disable gravatar support
# gravatar: false
# by default packages are ordercer ascendant (asc|desc)
# sort_packages: asc
# convert your UI to the dark side
# darkMode: true

# translate your registry, api i18n not available yet
# i18n:
# list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations
# web: en-US

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
# 代理公有镜像地址配置,由于npm很慢,可以改为淘宝或者cnpm,当这两个找不到时再改回npm吧
uplinks:
npmjs:
url: https://r.cnpmjs.org/

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

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

# You can specify HTTP/1.1 server keep alive timeout in seconds for incoming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enough.
# 增加监听端口号的配置,使内网都能访问到
listen: 0.0.0.0:4873
server:
keepAliveTimeout: 60

middlewares:
audit:
enabled: true

# log settings
logs:
- { type: stdout, format: pretty, level: http }

使用 verdaccio

结束掉 verdaccio以后, 使用pm2来后台运行 verdaccio,然后通过nrm修改本地所使用的镜像地址

1
2
3
4
5
6
7
pm2 start verdaccio
nrm add localnpm http://192.168.x.x:4873
nrm use localnpm
# 注册一个用户
npm adduser –-registry http://192.168.x.x:4873 --scope=@myOrg
# 进入具体的待发包文件夹以后执行以下命令,即可发布包到本地的镜像
npm publish
文章作者: pengweifu
文章链接: https://www.pengwf.com/2020/12/01/other/TOOL-verdaccio/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 麦子的博客
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论