搭建npm私库(超简单)

栏目: Node.js · 发布时间: 4年前

内容简介:我搭私库的原因很简单,目前正在开发一个组件库,提供给公司内部使用,我不想去注册npm,也不想等待npm的审核,只想要有个仓库快速测试发布自己的npm包。目前最方便的方案就是接下来详细介绍搭建的步骤。

原因

我搭私库的原因很简单,目前正在开发一个组件库,提供给公司内部使用,我不想去注册npm,也不想等待npm的审核,只想要有个仓库快速测试发布自己的npm包。

怎么搭

目前最方便的方案就是 verdaccio ,搭建非常方便,一般就几分钟就搞定了,需要的工具:

  • 安装nodejs和npm
  • 全局安装verdaccio
  • shh和pm2(非必须,如果你要部署到远程服务器的话)

接下来详细介绍搭建的步骤。

全局安装verdaccio

安装verdaccio之前,我默认大家都已经安装了nodejs和npm环境,这个就不再赘述了。如果是本地搭建的话,直接进行下面的操作就可以了。如果是在远程服务器搭建,通过ssh连接远程服务器就行。

# 全局安装
npm install verdaccio - g

修改verdaccio配置

修改配置的目的就是让我们的私库可以通过公网的ip访问,首先查看npm全局安装包的所在位置:

npm root -g
/usr/local/Cellar/node/8.4.0/lib/node_modules

其中 /usr/local/Cellar/node/8.4.0/lib/node_modules 便是我们npm包全局安装的地址。按以下命名查找配置文件所在的位置

搭建npm私库(超简单)

然后

vim default.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:
  # 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
    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?)
    publish: $authenticated

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

# You can specify HTTP/1.1 server keep alive timeout in seconds for incomming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enought.
server:
  keepAliveTimeout: 60

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

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

# listen
listen: 0.0.0.0:4873

最后一行为新增的配置, 用于支持外网ip访问

listen: 0.0.0.0:4873

然后输入 :wq 保存并退出vim模式,启动verdaccio服务即可。我一般会通过pm2启动,原因很简单,关闭doc窗口后,服务不会停掉,并且能很好的管理我们启动的服务。

使用pm2

常用命令

  • 安装:npm install pm2 -g
  • 启动:pm2 start verdaccio
  • 停止:pm2 stop verdaccio
  • 重启:pm2 restart verdaccio
  • 删除应用:pm2 delete verdaccio
  • 查看日志:pm2 logs verdaccio

我们来启动服务,查看效果:

pm2 start verdaccio

搭建npm私库(超简单)

至此,npm的私库搭建就完成了,图中是我最新发布的一个基于vue的组件库,后续会对组件库的编写和发布做介绍,有兴趣的朋友关注以下。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

跨境电商——阿里巴巴速卖通宝典

跨境电商——阿里巴巴速卖通宝典

速卖通大学 编著 / 电子工业出版社 / 2015-1 / 69.00元

跨境电商作为中国电子商务发展的最新趋势,受到了全社会越来越多的重视,大量中国卖家借助阿里巴巴速卖通平台,将产品直接售卖到全球的消费者手中,通过这条短得不能再短的交易链,获得了丰厚的回报。 但同时,跨境电商这一贸易形式,对卖家的综合素质要求比较高:卖家要对海外市场比较熟悉,对跨境物流有所把握,能够用外语进行产品介绍和客户交流,通过跨境结算拿到货款……诸如此类的门槛,让不少新卖家心生畏难,而所有......一起来看看 《跨境电商——阿里巴巴速卖通宝典》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具