npm私库搭建

Posted by Youzi on December 23, 2020

npm 私库搭建

npm 私库

公共组件和方法越来越多,项目又不能直接公开到 npm 官方库上,所以衍生出了搭建团队内部的npm私库,本文记录搭建过程以及踩坑,包括在服务器上的配置,发布测试 npm 包等等。

Verdaccio

安装

Verdaccio是一个私库搭建工具,在服务器上用npm进行安装;步骤如下,服务器上需要有node, npm环境:

1
npm install -g verdacciod

这一步可能会有报错,Linux 权限之类的问题,推荐使用以下命令

1
npm install -g verdaccio --unsafe-perm

cnpm, yarn之类的包管理命令也都能装,主要是要全局安装;备注一下服务器上的安装目录:/全局node包/lib/node_modules/verdaccio,这里的根目录是在npm全局包下,可以用npm config get prefix查看;

启动服务

安装好之后,只需要输入命令:

1
verdaccio

即可启动服务,一般不改动默认配置的话,是不会报什么错误的,如果有报错可以查看打印的日志找 bug,一般都是配置出错;

备注:我把 Verdaccio 配置的uplinks改成了阿里的源,再运行就报错了,暂时没去查是为啥;

1
2
3
4
5
 warn --- config file  - /root/.config/verdaccio/config.yaml
 warn --- Verdaccio started
 warn --- Plugin successfully loaded: verdaccio-htpasswd
 warn --- Plugin successfully loaded: verdaccio-audit
 warn --- http address - http://0.0.0.0:4873/ - verdaccio/4.9.1

第一行打印出来的就是Verdaccio的配置目录,可以在里面修改默认配置;

修改默认配置

首先我们的服务是运行在服务器的,而默认配置启动的服务路径是127.0.0.1:4873,只允许在服务器本地访问,所以我们修改默认配置:

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
# 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
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
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
#	url: https://registry.npm.taobao.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.
server:
  keepAliveTimeout: 60

middlewares:
  audit:
    enabled: true

# log settings
logs:
  - { type: stdout, format: pretty, level: http }
  #- {type: file, path: verdaccio.log, level: info}
#experiments:
#  # support for npm token command
#  token: false
#  # support for the new v1 search endpoint, functional by incomplete read more on ticket 1732
#  search: false

# This affect the web and api (not developed yet)
#i18n:
#web: en-US

# ---------------高亮------------
# 这条修改了启动Verdaccio服务的路径,把本地路径改成了全0,这样就能通过服务器ip访问到Verdaccio的网页端了
listen: 0.0.0.0:4873

# Verdaccio项目全局的相对路径,你可以自己改成别的,例如下面的,真实的网页端访问路径为:
# http://服务器ip:4873/verdaccio/
url_prefix: /verdaccio/
# 设置发布包的大小
max_body_size: 100mb
#listen: 127.0.0.1:4873
  • 修改了listen,这是启动 Verdaccio 服务的路径,把本地路径改成了全 0,这样就能通过服务器 ip 访问到 Verdaccio 的网页端了;
  • 修改了url_prefix,这是Verdaccio服务的前缀,我的服务器不仅对外提供Verdaccio服务,所以我改了这个,这样Verdaccio的网页端访问路径为:http://server-ip:4873/verdaccio/,服务器 ip+前缀;
  • 修改了默认发布包的大小,这是个坑,后面讲。

利用 pm2 守护进程

现在我们做到了向外提供服务,但是难免会有其他状况导致Verdaccio服务停止,所以我们用另一个包pm2来守护这个服务进程;pm2也是一个npm的包,所以直接安装:

1
npm install -g pm2

启动:

1
pm2 start verdaccio

停止:

1
pm2 stop verdaccio

查看状态

1
pm2 show verdaccio

配置 nginx 进行转发(可选)

不想写端口咋办,用 nginx 转发一下,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
location /verdaccio/ {
  proxy_pass http://服务器ip:4873/;
  proxy_set_header Host            $host;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header X-Forwarded-Proto http;
}
location /-/ {
  proxy_pass http://服务器ip:4873/-/;
  proxy_set_header Host            $host;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header X-Forwarded-Proto http;
}

第一项配置的location要与config.yaml里的url_prefix保持一致,后一项配置参考的是https://blog.csdn.net/lunahaijiao/article/details/107572843

本地使用

nrm

首先在本地环境安装一个全局包nrm,用于管理本地所有的npm源,一般有几个默认存在的,比如官方的源,yarn的源;nrm的使用很简单,就几个基本命令,use, add, ls, del对应增删查改,一查就会咯。

1
npm install -g nrm

添加私库源,这里可以随便起名字,我用的就是verdaccio代表了私库的名字

1
2
3
nrm add verdaccio http://服务器ip/verdaccio/

nrm use verdaccio

发包到私库

其实也遵循了一般的发布到公共库的规则,建立项目,也可以使用已有项目,反正就是要发布的项目咯:

1
2
3
4
5
6
7
8
9
10
11
12
13
npm init

# 添加用户,依次输入用户名密码
npm adduser

# 如果已经有了账号,也是一次输入用户名密码
npm login

# 注意,发布前要切换到npm官方源
npm use npm

# 正式发布包到私库
npm publish --registry http://服务器ip/verdaccio/

注意:最好nodenpm的版本都高一些,node高于6.0.0,npm高于3.0.0,不然可能有报错

发布完了之后我们可以在网页端查看自己发的包了,地址:http://服务器ip/verdaccio/

发布踩坑备注

备注:我发布的时候遇到一个报错,413 Payload Too Large,从log提示可以看出request entity too large应该是express服务器报错了,上传的包太大了,这时候就想起了刚刚配置Verdaccio时加上的配置项,这是设置最大能发布的包的大小的配置项,详见这一条issue

1
2
# 设置发布包的大小
max_body_size: 100mb
1
2
code E413
npm ERR! 413 Payload Too Large - PUT 包的地址 - request entity too large