npm包开发及私有仓库配置使用

原因:一个项目多个系统之间存在可复用的代码,重复的复制粘贴及修改,偶尔也会遗漏。集中管理和维护,提升代码复用率和维护效率

开发npm包

初始化项目

生成package.json文件

csharp 复制代码
npm init -y
json 复制代码
{
  "name": "product-npm", // 包名称
  "version": "1.0.0", // 包版本信息
  "description": "",
  "main": "dist/index.js", // 包的入口文件
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "rollup --config"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

编写代码内容及打包

安装 rollupjs 进行打包,生成dist文件夹,内容存放在该文件夹下。 main的值也需根据输出地址进行变更

选择 verdaccio 进行私有仓库管理

全局安装verdaccio

cmd 复制代码
npm i verdaccio -g

检查是否安装成功

显示版本号即为成功

cmd 复制代码
verdaccio -v

运行 verdaccio

cmd 复制代码
verdaccio

终端会显示 config file 和 http address

config file - 配置文件的地址

htpp address - 仓库的管理界面

添加用户

执行命令后,根据终端的提示设置用户名、密码、邮箱

cmd 复制代码
npm add user --registry http://localhost:4873/
# --registry http://localhost:4873/ 指定仓库源

如出现报错需要检查配置文件中max_users是否有设置(默认被注释了)且值不能为1

yaml 复制代码
auth:
  htpasswd:
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration. 如果为-1禁止注册
    max_users: 1000
    # Hash algorithm, possible options are: "bcrypt", "md5", "sha1", "crypt".
    # algorithm: bcrypt # by default is crypt, but is recommended use bcrypt for new installations
    # Rounds number for "bcrypt", will be ignored for other algorithms.
    # rounds: 10

登录用户

arduino 复制代码
npm login --registry http://localhost:4873/

发布\更新包

在包根目录执行

arduino 复制代码
npm publish --registry http://localhost:4873/

如出现报错需要检查package.json文件下version是否有变更

删除包

css 复制代码
npm unpublish product-npm[包名] --registry http://localhost:4873/

使用

由于verdaccio部署在自己的电脑下,所以在项目里直接 install 即可

css 复制代码
npm i product-npm --registry http://localhost:4873/

同事需要使用时,则将localhost变为对应的我的电脑的ip

css 复制代码
npm i product-npm --registry http://192.168.1.6:4873/

如出现无法安装需要检查配置文件中listen参数,开启0.0.0.0,以致可通过ip访问

shell 复制代码
# 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
相关推荐
夏幻灵4 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_5 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝5 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions5 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发5 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_5 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞055 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、5 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao5 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly5 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强