windows下使用verdaccio构建npm私服环境

一.背景

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-perm

added 262 packages in 34s

38 packages are looking for funding
  run `npm fund` for details
npm notice
npm notice New minor version of npm available! 10.2.4 -> 10.4.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.4.0
npm notice Run npm install -g npm@10.4.0 to update!
npm notice

C:\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 socket
listen:
- 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 to
uplinks:
  npmjs:
#    url: https://registry.npmjs.org/
    url: https://mirrors.huaweicloud.com/repository/npm/

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

相关推荐
roamingcode16 小时前
Claude Code NPM 包发布命令
前端·npm·node.js·claude·自定义指令·claude code
gc_229917 小时前
运行npm run命令报错“error:0308010C:digital envelope routines::unsupported”
npm·node.js
zz-zjx20 小时前
Nodejs 与npm
前端·npm·node.js
ChinaLzw1 天前
配置npm国内源(包含主流npm镜像源地址)
npm·node.js
辉长六加11 天前
nodejs和vue安装步骤记录
前端·javascript·vue.js·npm·node.js
SuperYing3 天前
还在为调试组件库发愁吗?yalc 帮你一把
前端·npm
棒棒的唐3 天前
nodejs安装后 使用npm 只能在cmd 里使用 ,但是不能在poowershell使用,只能用npm.cmd
前端·npm·node.js
字节逆旅4 天前
nvm 安装pnpm的异常解决
前端·npm
DevSecOps选型指南4 天前
SBOM风险预警 | NPM前端框架 javaxscript 遭受投毒窃取浏览器cookie
前端·人工智能·前端框架·npm·软件供应链安全厂商·软件供应链安全工具
lovepenny5 天前
Failed to resolve entry for package "js-demo-tools". The package may have ......
前端·npm