npm包管理,选择verdaccio,OK!

496 阅读2分钟

前言

随着公司项目数量的增加,使用私有npm(Node Package Manager)仓库的重要性日益凸显,个人总结以下几个方面:

  1. 私有依赖:私有npm仓库允许公司存储和共享内部开发的私有模块和依赖,这些模块可能包含敏感代码或专有技术
  2. 版本控制:私有npm仓库可以帮助公司更好地管理依赖的版本,确保不同项目之间依赖的一致性和兼容性
  3. 自动化集成:私有npm仓库可以与公司的CI/CD流程集成,自动化依赖的更新和部署
  4. 减少外部依赖:通过减少对外部npm仓库的依赖,可以降低因外部服务中断或变更而带来的风险
  5. 共享和重用:团队成员可以更容易地共享和重用代码,提高开发效率

如何选择

常见的开源npm管理仓库:1.Nexus 2.Verdaccio 本文主要使用Verdaccio来完成从0到1的过程

开始搭建

  • 继续使用docker服务,Dockfile如下:

# The final built image will be based on the standard Verdaccio docker image.
FROM verdaccio/verdaccio:5
# 此处需要换成服务器ip或者域名
ENV VERDACCIO_PUBLIC_URL=http://xx.xx.xx.xx
USER root

RUN npm config set registry https://registry.npmmirror.com/
RUN npm install --global verdaccio-static-token \
  && npm install --global verdaccio-auth-memory
USER $VERDACCIO_USER_UID

RUN chmod 777 -R /verdaccio/storage
  • 目录结构:

文件结构.jpg

  • 如上图目录结构下,使用如下步骤创建服务:
# 1.创建镜像
# docker build -t verdaccio .
# 2.创建容器
# sudo docker run -itd --name my-verdaccio -p  4873:4873 -v /home/ycc/verdaccio/conf:/verdaccio/conf  --restart=always verdaccio


请注意,如上配置中
1.Dockerfile中的环境变量VERDACCIO_PUBLIC_URL
2.config.yaml中的url_prefix: '/npm'
均是为了使用反向代理(解决问题12),如果不想使用反向代理,去掉这两个配置即可

遇到了哪些问题

1.不想直接使用127.0.0.1:4873怎么办

使用反向代理是一种常见的做法,如官方文档所述:

Using a reverse proxy is a common practice. The following configurations are the most recommended and used ones.

Important, the headers are considered to resolve the public are X-Forwarded-Proto for the protocol and Host for the domain, please include them in your configuration.

<VirtualHost *:80>  
AllowEncodedSlashes NoDecode  
ProxyPass /npm http://127.0.0.1:4873 nocanon  
ProxyPassReverse /npm http://127.0.0.1:4873  
</VirtualHost>
  1. 以上配置为apache配置反向代理,将访问本地带/npm的请求转发到http://127.0.0.1:4873
  2. 最终暴露给使用方的服务地址为 registry=http://{serverIp}/npm
2.TAR_ENTRY_INVALID checksum failure
npm WARN tar TAR_ENTRY_INVALID checksum failure  
npm WARN tar zlib: incorrect data check

问题原因:gzip压缩时出现的问题,是几率性的,可能还会导致服务奔溃

解决办法:使用反向代理,配置中增加 SetEnv no-gzip 1

<VirtualHost *:80>  
AllowEncodedSlashes NoDecode  
SetEnv no-gzip 1  
ProxyPass /npm http://127.0.0.1:4873 nocanon  
ProxyPassReverse /npm http://127.0.0.1:4873  
</VirtualHost>
3.zlib check failed

原因分析:

  1. 官方文档写了安装verdaccio的最低要求 >=node 18
  2. 项目的node版本和verdaccio6的匹配程度,没有找到相关文档,目前老项目用14版本,使用verdaccio6不成功
  3. 将verdaccio6回退到verdaccio5,既可以正常使用了
  4. 在verdaccio5版本下,使用node11到node20均已正常使用

解决办法:通过以上步骤可以判断,是verdaccio版本与当前使用项目node(npm)版本不兼容;

4.项目中npm install时不在私有npm服务中的包,默认会向npmjs继续寻找,如何配置代理

在config.yaml中配置如下:


# a list of other known repositories we can talk to
uplinks:
  taobao:
    url: https://registry.npmmirror.com/
  npmjs:
    url: https://registry.npmjs.org/
  yarn:
    url:  https://registry.yarnpkg.com/
    
packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: taobao

  '**':
    # 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/unpublish packages
    # (anyone can register by default, remember?)
    publish: $authenticated
    unpublish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: taobao

关键是配置proxy: taobao

5.有版本/包发布时,如何设置提醒?

config.yaml中配置项Notify

...
notify:
  method: POST
  headers: [{ 'Content-Type': 'application/json;charset=utf-8' }]
  endpoint: https://oapi.dingtalk.com/robot/send?access_token=xxxxx
  content: '{"msgtype":"actionCard", "actionCard": {"text": "package published: * {{ name }} * publisher name: * {{ publisher.name }} *  version: {{#each versions}} v{{version}}{{/each}} "}}'

解释:

  1. 以上配置是verdaccio提供的消息配置项,endpoint是你需要接入提醒的地址,content是接入api定义的内容:

见官方文档如下章节:

Notifications | Verdaccio

  1. content中的写法是配合endpoint来的,因为接入的是钉钉,则需要看钉钉消息体的文档

钉钉消息文档: 自定义机器人发送群消息 - 钉钉开放平台 (dingtalk.com)

通知成功:

message-success.jpg

完整代码见如下git仓库:

yc-lm/document-helper (github.com)

参考文档

  1. Reverse Proxy Setup | Verdaccio
  2. 自定义机器人发送群消息 - 钉钉开放平台 (dingtalk.com)