npm私有仓库搭建(验证OK)

核心:采用verdaccio,Verdaccio 是一个 Node.js创建的轻量的私有npm proxy registry

安装verdaccio

1、首先全局安装verdaccio:

npm install -g verdaccio

2、启动verdaccio:用cmd命令,在路径C:\Users\用户名\AppData\Roadming\npm下,执行 verdaccio即可启动仓库

3、配置verdaccio(启动后才有config.yaml文件):

配置文件地址:C:\Users\Administrator\AppData\Roaming\verdaccio\config.yaml

修改内容:重点是以下两行

listen: http://IP:4873

url: https://registry.npm.taobao.org/

#

# 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

# 缓存路径,最好找一个空间比较大的盘来放,默认是在C 盘

storage: D:\verdaccio\storage

# path to a directory with plugins to include

plugins: ./plugins

web:

  title: Verdaccio

  # comment out to disable gravatar support

  # gravatar: false

  # by default packages are ordercer ascendant (asc|desc)

  # sort_packages: asc

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.npm.taobao.org/

packages:

  '@*/*':

    # scoped packages

    access: $all

    publish: $authenticated

    unpublish: $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/publish packages

    # (anyone can register by default, remember?)

    publish: $authenticated

    unpublish: $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 incoming 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 enough.

server:

  keepAliveTimeout: 60

middlewares:

  audit:

    enabled: true

# log settings

logs:

  - { type: stdout, format: pretty, level: http }

  #- {type: file, path: verdaccio.log, level: info}



#需要监听的端口,IP

listen: http://IP:4873

#最大的文件包限制

max_body_size: 300mb

#experiments:

#  # support for npm token command

#  token: false

# This affect the web and api (not developed yet)

#i18n:

#web: en-US

4、关闭启动窗口,重新启动

5、直接打开http://IP:4873地址,这就是你的仓库

6、添加账号:npm adduser --registry=http://IP:4873,按照提示定义用户名、密码和邮箱

7、登录:npm login,按照提示输入用户名、密码和邮箱

8、发布:找到已经开发的工程,直接在路径下直接执行命令:npm publish

如下便是发布成功

image

刷新http://IP:4873地址,能看到自己发布的包

image

正常流程到此就算是结束了~~~~~~不过,下面两个也必不可少哦

安装nrm

nrm是一个npm的资源管理器,允许快速的在npm源之间切换

1、安装:

npm install -g nrm

2、添加自己的npm 镜像源,register是镜像源名称,http://IP:4873是url:

nrm add register http://IP:4873

查看镜像源:

nrm ls

image

3、切换到添加的镜像源:

nrm use register

4、删除镜像

nrm del <registry>

安装守护进程pm2

守护进程pm2,防止异常情况引起的verdaccio服务停止

1、安装:

npm install pm2 -g

2、启动verdaccio

pm2 start C:\Users\shirunxiang.HOLLYSYS\AppData\Roaming\npm\node_modules\verdaccio\bin\verdaccio --name verdaccio

参考:搭建NPM私有库 - 简书

使用自己发布的包

2021.7.6更新

内网搭建的仓库,如果内网突然断网的话,还要继续能push包,则需要在yaml文件的最后,增加如下代码,然后重启:

publish

allow_offline: true
没有网络环境下搭建verdaccio(内网搭建)(未测试)

将以下对应的外网目录拷贝到内网环境中

文件:C:\Users\用户名\AppData\Roaming\npm\verdaccio

文件:C:\Users\用户名\AppData\Roaming\npm\verdaccio.cmd

目录:C:\Users\用户名\AppData\Roaming\npm\node_modules\verdaccio

目录:C:\Users\用户名\AppData\Roaming\verdaccio

注意,其中 storage 目录是存放npm依赖包的地方, 我们可以先直接在外网发布好npm包,然后把storage文件夹复制到内网,接着打开内网verdaccio地址,就能发现这些依赖包自动发布到内网了。

我们发布依赖包到npm私服,有两种包,一种是自己开发的包,另一种是外网npm上的开源包,如果想要将开源包发布到自己的私服上使用,请注意一下几点(踩着坑过来的!!!):

1: 要发布的npm依赖包,最好用npm下载,别用cnpm,因为cnpm包含了各种快捷方式,拷贝到其他电脑会出现各种问题

2: 注意查看发布的npm包中package.json中的 script 中是否含有 republish等钩子属性,republish 会在发布之前执行该命令,而往往我们并不具备执行该命令的环境,从而报错,我们可以将它删除

3:依赖包中有可能嵌套了其他依赖包,该依赖包中子目录的 node_modules文件夹查看依赖情况,发布之前也必须一并将它发布。因为 node_modules 是冒泡向上查找依赖的,相同依赖包有不同版本,公共版本被提取到了依赖包的最外层,特殊版本只能存在于当前目录底下 node_modules文件夹

4:如果想发布指定 package.json 下的所以npm包,正常情况下至少有100多个依赖包,我们手动发布是很辛苦的。但是, verdaccio又不支持一键导入,所以我们只能一个个发布。这里我写了一个 node自动读取 node_modules 目录下的文件并且自动发布依赖包到 npm 的publish 的文件,供大家试用!

修改配置

# listen port
listen: 0.0.0.0:4873

 了解更多内容 关注公众号

  • 30
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值