美文网首页
前端模块化知识准备:NPM私有服务器手顺

前端模块化知识准备:NPM私有服务器手顺

作者: 我也曾想起个好名儿 | 来源:发表于2020-09-22 08:40 被阅读0次

    因为考虑到模块化,同一代码规格,并且把统一的模块抽离出来统一管理,又不希望业务代码散播到npm上,我们需要自己搭建对应npm仓库。
    网上有很多关于Sinopia的安装说明,但是从它的git维护状态来看(已经停止维护两年),短期内被大家弃用是显而易见的事情。

    最近大家用的比较多的是 nrm + Verdaccio的方案。为了方便之后回顾,所以记录了以下手顺。

    配置工作

    • 安装 nrm
      npm install nrm -g
      
    • 安装Verdaccio
      npm install verdaccio -g
      
    • 配置,增加淘宝的代理
      uplinks:
        taobao:
          ...
          url: https://registry.npm.taobao.org/
          ...
      packages:
          ...
          proxy: taobao
      

    通过以上几步,前期的准备工作就完成了,通过下面的命令可以将Verdaccio运行起来:

      verdaccio
    

    服务的默认WebUI地址为http://localhost:4873

    当我们有需要上传的模块的时候, 以下是几个常用命令:

      # 增加登录用户
      npm adduser --registry http://localhost:4873
      # 发布模块
      npm publish --register http://localhost:4873
      # 为nrm增加一个仓库路径 
      nrm add verdaccio http://localhost:4873
      # 通过nrm 切换当前仓库路径,结合上一条
      nrm use verdaccio
    

    编写模块

    模块的定义使用Common JS的模块定义方式。任何lib类型的node项目都可以发布。

    引用模块

    引用前需要使用nrm切换仓库至本地仓库:

      nrm use verdaccio
    

    然后使用npm安装后,通过常规方式引用就可以了:

      npm install -S [模块名]
    

    相关文章

      网友评论

          本文标题:前端模块化知识准备:NPM私有服务器手顺

          本文链接:https://www.haomeiwen.com/subject/jxavyktx.html