使用 verdaccio 搭建一个 npm 私服

96 阅读1分钟

使用 verdaccio 搭建一个 npm 私服

一、安装

npm install -g verdaccio

二、启动

verdaccio
# info --- Creating default config file in C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml
# warn --- config file  - C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml
# warn --- "crypt" algorithm is deprecated consider switch to "bcrypt". Read more: https://github.com/verdaccio/monorepo/pull/580
# info --- plugin successfully loaded: verdaccio-htpasswd
# info --- plugin successfully loaded: verdaccio-audit
# warn --- http address - http://localhost:4873/ - verdaccio/5.15.3

可以看到,我们本地的私服地址是 http://localhost:4873/

使用 pm2 启动 verdaccio。

npm install pm2 -g

# 启动
pm2 start C:\Users\Administrator\AppData\Roaming\npm\node_modules\verdaccio\bin\verdaccio

# 重启
pm2 restart verdaccio

添加用户

# 添加用户
npm adduser --registry http://localhost:4873/

修改配置文件C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml

# 修改下述配置,然后开放服务器端口即可直接从外网访问UI页面
# 需要注意的是 重启后第一次访问必须在外网访问
# 否则页面生成的资源(js等)路径都是本地(localhost)的

auth:
  htpasswd:
    file: ./htpasswd
    max_users: -1 # 禁止注册
    
max_body_size: 100mb

listen: 0.0.0.0:4873
# - 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

i18n:
  web: zh-CN

三、发布包

# 首先登录上方注册的用户
npm login --registry http://localhost:4873/
# 在项目根目录下执行
npm publish --registry http://localhost:4873/
# + test@1.0.0
# 取消发布包 在项目根目录下执行
npm unpublish -f --registry http://localhost:4873/
# - test@1.0.0
# 删除后去页面查看发现还能看到
# 但是显示的是上游仓库包的链接
# 实际安装不受影响

The End, Last Updated by Jimmy, 15 Sep 2022.