Linux 中使用 verdaccio 搭建私有npm 服务器

本文指导如何在Linux中安装Verdaccio私有NPM镜像,配置config.yaml,使用pm2确保服务常驻,以及设置npm指向本地镜像。还介绍了用户注册、登录、发包和删除包的步骤,以及防火墙和安全组的设置。

安装 Node

Linux中安装Node

安装verdaccio

npm i -g verdaccio

安装完成 输入verdaccio,出现下面信息代表安装成功,同时输入verdaccio后verdaccio已经处于运行状态,当然这种启动时暂时的,我们需要通过pm2让verdaccio服务常驻

   yg@iZ2zec61wsgbo9t9i346jbZ:~$ verdaccio
   warn --- config file  - /home/yg/.config/verdaccio/config.yaml
   warn --- Plugin successfully loaded: htpasswd
   warn --- Plugin successfully loaded: audit
   warn --- http address - http://localhost:4873/ - verdaccio/3.10.1

配置config.yaml

1、vim /home/yg/.config/verdaccio/config.yaml 进入编辑配置文件

# 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服务,能够通过web访问
web:
  # WebUI is enabled as default, if you want disable it, just uncomment this line
  #enable: false
  title: Verdaccio

# 验证信息
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/

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    # 代理,表示没有的仓库去这个npmjs里边去找
    # npmjs 又指向 https://registry.npmjs.org/ ,就是上面的 uplinks 配置
    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 packages
    # (anyone can register by default, remember?)
    # 发布package的权限
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    # 如果package不存在,就向代理的上游服务器发起请求
    proxy: npmjs

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true

# 监听端口,重点,不配置这个只能本机可以访问
listen: 0.0.0.0:4873

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: verdaccio.log, level: info}

安装 pm2

npm i pm2 -g

启动

pm2 start verdaccio  

就可以通过http://xxx(ip地址)去访问了

在这里插入图片描述
若无法访问,查看下端口是否已打开

#查看防火墙规则
firewall-cmd --list-all
#查询端口是否开放
firewall-cmd --query-port=4873/tcp(默认端口,可以自定义)
#开放4873端口
firewall-cmd --permanent --add-port=4873/tcp (默认端口,可以自定义)
#重新载入 
firewall-cmd --reload
阿里云服务器请开放安全组端口

设置 npm 服务指向本地

npm config set registry http://localhost:4873

注册用户

# 注册用户 在本地注册一个用户然后指向我们的地址然后我们就可以发布包了
npm adduser --registry http://xxx:4873
Username: xxx
Password: xxx
Password:  xxx
Email: (this IS public) xxx
Logged in as yg-ui on http://xxx/ (你的ip地址)
这时候我们就注册一个用户,我们可以用这个用户名和密码去登录去上图窗口去登录了

登录

npm login 	# 用刚刚注册的用户名密码进行登录
npm whoami	# 查看当前登录的账号

发包

mkdir npm-publish-test	# 包文件夹
cd npm-publish-test
npm init				# npm初始化
npm publish	

删除 指定版本或包

$ npm unpublish [<pkg>][@<version>] --force

下面列一下几种使用场景

  • $ npm unpublish dzmtest@1.0.1 --force
    直接强制删除指定包的指定版本,不需要 cd 进入包文件夹内,随处可以执行。

  • $ npm unpublish dzmtest --force
    直接强制删除指定包,不需要 cd 进入包文件夹内,随处可以执行。

### Verdaccio NPM Private Registry Setup and Usage #### Installation of Verdaccio To begin with, installing Verdaccio can be done using npm globally which allows running it from anywhere within the system. The command used for this purpose is `npm install -g verdaccio`[^1]. Once installed, starting Verdaccio involves executing the command `verdaccio`, leading to its operation on port 4873 by default. #### Configuration File Overview The configuration file (`config.yaml`) plays a crucial role in setting up Verdaccio properly. This YAML-based file contains settings such as storage paths, listen addresses, authentication methods, and more. For instance, configuring basic HTTP authentication requires specifying user credentials inside this file under the auth section[^2]. #### Publishing Packages Privately Publishing packages privately through Verdaccio follows similar conventions found when publishing public modules via official registries like npmjs.org. However, instead of pushing directly there, developers target their local Verdaccio server URL during publication. An example publish command might look like so: ```bash npm publish --registry=http://localhost:4873/ ``` This ensures that only users authenticated against your private repository have access rights over these artifacts[^3]. #### Using Private Packages Within Projects For projects wishing to consume internally developed libraries stored within Verdaccio instances, modifying `.npmrc` files becomes necessary. Adding lines pointing towards custom repositories enables seamless integration without altering existing workflows significantly. A typical entry looks something along those lines: ```properties @myscope:registry=https://my.verdaccio.server.com //my.verdaccio.server.com/:_authToken=${VERDACCIO_TOKEN} ``` Here `${VERDACCIO_TOKEN}` represents environment variables holding tokens required for accessing protected resources hosted at specified URLs[^4]. #### Security Considerations Security remains paramount while operating any kind of service exposed online including self-hosted package managers. Implementing measures ranging from secure password policies, restricting network exposure appropriately, employing HTTPS connections wherever possible helps mitigate potential risks associated with unauthorized accesses or data breaches[^5].
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值