一.背景

npm太慢了,每次jenkins自动构建等太久,我虽然是后端coder,也看不惯。本文目的只是说明怎么搭建npm私服,我现在只想构建快一点。所以,暂时没有考虑多个开发者将自定义组件上传到私库并共享的问题,以后有时间再说吧。

二.npm环境准备

详见我之前的文章windows系统下安装nodejs-CSDN博客

三.安装verdaccio

输入命令安装

npm install -g verdaccio --unsafe-perm

安装正常情况还是很快,大概30秒以内,结果如下

Microsoft Windows [版本 10.0.17763.107](c) 2018 Microsoft Corporation。保留所有权利。C:\Users\Administrator>npm install -g verdaccio --unsafe-permadded 262 packages in 34s38 packages are looking for fundingrun `npm fund` for detailsnpm noticenpm notice New minor version of npm available! 10.2.4 -> 10.4.0npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.4.0npm notice Run npm install -g npm@10.4.0 to update!npm noticeC:\Users\Administrator>

四.启动verdaccio

在刚刚的cmd中,继续输入verdaccio,启动大概10秒左右。结果如下

C:\Users\Administrator>verdaccio(node:2296) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.(Use `node --trace-deprecation ...` to show where the warning was created) info --- config file- C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml info --- the "crypt" algorithm is deprecated consider switch to "bcrypt" in the configuration file. Read the documentation for additional details info --- using htpasswd file: C:\Users\Administrator\AppData\Roaming\verdaccio\htpasswd info --- plugin successfully loaded: verdaccio-htpasswd info --- plugin successfully loaded: verdaccio-audit warn --- http address - http://localhost:4873/ - verdaccio/5.29.0

五.修改访问地址

按照上面的提示,在浏览器输入http://localhost:4873/可以看到如下界面,说明启动是成功的。

但是,当我们用ip访问就不行了。

这是因为,监听的是localhost,要修改监听为0.0.0.0 。因为安装好了后别人要使用啊,肯定不会输入localhost来连接我们的私服,所以要修改,是的ip也可以访问。

根据上一步中启动后的提示,我们知道配置文件的位置如下

info — config file – C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml

找到文件,在文件中搜索listen,找到对应的注释这一段后面,按照注释给你的规范,配置输入 listen: – 0.0.0.:4873

(在注释的位置后面做配置是良好的习惯)

# https://verdaccio.org/docs/configuration#listen-port# 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 socketlisten:- 0.0.0.0:4873

修改后,重启一下(重启不应该我还要记录吧。。。在刚刚的cmd中,ctrl+c,按小键盘的上或者重新输入verdaccio,敲回车),就能ip访问了。

六.修改仓库位置

想指定verdaccio组件存储位置,还是在上一步的文件中,找到storage,有默认的配置。可以不修改哈,默认就可以。

七.指定代理仓库

这个是解决我构建慢的最关键步骤。我们安装verdaccio后,我们的仓库中空空的,如果别人要使用我们就下载不到东西,所以,我们还要指定代理仓库。因为npm在国内用起来很慢,所以我们指定上级为大厂的地址。阿里的也勉强,我更喜欢华为的。还在在之前的config.yaml文件中,找到uplinks。输入配置如下:

# https://verdaccio.org/docs/configuration#uplinks# a list of other known repositories we can talk touplinks:npmjs:#url: https://registry.npmjs.org/url: https://mirrors.huaweicloud.com/repository/npm/

我是先注释了原来的上级代理仓库配置(行前面加#就是注释,你不会不知道吧?),然后复制了一行写上了华为的代理。