npm私服搭建过程(一)

776 阅读4分钟

技术选用背景

在公司如火如荼的进行数字化转型的过程中,技术团队不可避免的要挑起大梁,做一些关键高效的事情,最近前端项目组进行合并规整,突然一下子要做的东西多了很多,npm私服搭建和npm自又包的建设要立项做起来。

技术实现选择

由于任务多时间紧迫,大家都是边做需求的同时边进行npm私有服务器的建设,目前前端搭建私服比较流行的是工具是verdacciosinopia ,由于sinopia的创建者不再维护改项目了,它的一个fork的大牛也就是verdaccio的作者一直维护着项目,全面考虑各种状态,我们选用了verdaccio作为私服的最终搭建工具。

Verdaccio优势

Verdaccio是一个简单的,零配置所需的本地私有npm注册表。不需要整个数据库就可以开始使用!Verdaccio开箱即用,拥有自己的小型数据库,能够代理其他注册表,一路缓存下载的模块。

搭建前环境准备

  • npm或yarn包管理器 版本最好是长期稳定版本
  • nrm npm的镜像源管理工具
  • pm2 node进程管理守护器 以上环境请自行配置,该文档就不做太多说明,ps: 网上资料很丰富的。

npm私服搭建步骤

  • 1,全局安装verdaccio
    • npm install --global verdaccio 或者 yarn add --global verdaccio
  • 2,修改配置文件config.yaml 文件位置:C:\Users\(用户名)\AppData\Roaming\verdaccio

image.png

由于默认配置的yaml文件解析不了, 只能放截图了,截图用的是VScode插件 PolaCode,进行全屏截的,有兴趣的伙伴可以试一试。 code.png

下面是网上常用的属性设置中文版配置

#设置NPM包的存放目录
storage: ./storage
#配置WEB UI界面
web :
  title : "搭建私有NPM"
  #logo : logo.png
#设置用户验证的文件。
auth:
  htpasswd:
  file: ./htpasswd
  max_users: 1000 #默认为1000,改为-1,禁止注册
#设置其它的npm注册源(registry)
uplinks:
  npmjs:
  url: https://registry.npmjs.org/
#配置权限管理
packages:
   '@/':
  #表示哪一类用户可以对匹配的项目进行安装 【$all 表示所有人都可以执行对应的操作,$authenticated 表示只有通过验证的人可以执行对应操作,$anonymous 表示只有匿名者可以进行对应操作(通常无用)】
  access: $all
  #表示哪一类用户可以对匹配的项目进行发布
  publish: $authenticated
  #表示哪一类用户可以对匹配的项目进行安装
  access: $all
  #表示哪一类用户可以对匹配的项目进行发布
  publish: $authenticated
  # 如果一个npm包不存在,它会去询问设置的代理。
  proxy: npmjs
#日志输出设置
logs:
   -{type: stdout, format: pretty, level: http}
  #-{type: file, path: verdaccio.log, level: info}
#修改监听的端口
listen: 0.0.0.0:4873
  • 3,启功私服
    • 执行命令 verdaccio
 PS C:\Users\ci21843> verdaccio
 warn --- config file  - C:\Users\ci21843\AppData\Roaming\verdaccio\config.yaml
 warn --- Verdaccio started
 warn --- Plugin successfully loaded: verdaccio-htpasswd
 warn --- Plugin successfully loaded: verdaccio-audit
 warn --- http address - http://192.168.14.139:4873/ - verdaccio/4.8.1
  • 打开http://192.168.14.139:4873/

image.png

  • 4,切换代理

    # 查看代理配置
    PS C:\Users\ci21843> nrm ls
     * npm -------- https://registry.npmjs.org/
     yarn ------- https://registry.yarnpkg.com/
     cnpm ------- http://r.cnpmjs.org/
     taobao ----- https://registry.npm.taobao.org/
     nj --------- https://registry.nodejitsu.com/
     npmMirror -- https://skimdb.npmjs.com/registry/
     edunpm ----- http://registry.enpmjs.org/
     # 添加本地环境镜像
     PS C:\Users\ci21843> nrm add ceshi http://192.168.14.139:4873/
     add registry ceshi success
     # 使用本地代理
     PS C:\Users\ci21843> nrm use ceshi
    Registry has been set to: http://192.168.14.139:4873/
    
  • 5,添加登录用户

# 查看当前用户信息
PS C:\Users\zt_1921> npm who am i
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`
# 注册
PS C:\Users\zt_1921> npm adduser
Username: code1921
Password:
Email: (this IS public) 1501613746@qq.com
# 登录
PS C:\Users\zt_1921> npm login
Username: code1921
Password: #密码输入不显示的
Email: (this IS public) 1501613746@qq.com
  • 6,发布包到Verdaccio环境
# 示例仓库
{
  "name": "npm-package-demo",
  "version": "1.0.0",
  "description": "my first test package",
  "main": "main.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [
    "npm",
    "package"
  ],
  "author": "code1921",
  "license": "ISC"
}
#发布前要定义好版本号
PS C:\Users\zt_1921> npm  publish

总结

这些资源都是网上可以搜寻的,但是自己在具体的操作中,还是会出现一点点问题的,有的命令是执行好多次才成功,好多时候是因为npm代理没有及时的切换,还有就是环境不太稳定,当发布完后有时候会自动的关掉npm服务,好像有文档指出要进行node 进程的守卫(pm2: node 守卫进程的命令工具),总的来说实现起来还是很简单的,下面会结合jenkins做一些CI/CD的事情。