• 0

  • 472

  • 收藏

npm私有服搭建(verdaccio)

6天前

基于windows平台,使用verdaccio搭建一个内网的npm私有服务器,用来管理公司前端团队的npm包。

一、verdaccio安装与相关配置

1.1 安装

使用 npm 安装 Verdaccio ,需要全局安装,所以注意权限问题。

npm install -g verdaccio
复制代码

1.2 运行

使用verdaccio命令启动运行

verdaccio
复制代码

运行结果

 warn --- config file  - C:/Users/Administrator/AppData/Roaming/verdaccio/config.yaml   # 配置文件路径
 warn --- Plugin successfully loaded: htpasswd
 warn --- Plugin successfully loaded: audit
 warn --- http address - http://localhost:4873/ - verdaccio/4.4.0
复制代码

浏览器访问 http://localhost:4873/ 即可打开npm仓库地址

1.3 配置

通过运行结果,找到配置文件路径 C:/Users/Administrator/AppData/Roaming/verdaccio/config.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: D:/npm-repo
# 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
  # convert your UI to the dark side
  # darkMode: true

# translate your registry, api i18n not available yet
# i18n:
# list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations
#   web: en-US

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
    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}
#experiments:
#  # support for npm token command
#  token: false
#  # support for the new v1 search endpoint, functional by incomplete read more on ticket 1732
#  search: false
#  # disable writing body size to logs, read more on ticket 1912
#  bytesin_off: false

# This affect the web and api (not developed yet)
#i18n:
#web: en-US
listen: 0.0.0.0:4873
复制代码

需要配置参数有以下几个

1.3.1 storage

存放npm包的存储地址,可以在本地磁盘中新建一个目录,单独进行存储。

# path to a directory with all packages
storage: D:/npm-repo
复制代码

1.3.2 uplinks

在本地npm包中找不到时,默认在其他仓库进行install,默认配置从npm官方拉取。

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
复制代码

1.3.3 packages

设定包发布与使用的权限,是否代理到公有npm仓库等

packages:
  '@xdh/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated

  '**':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs
复制代码

@xdh前缀的包为私有包,不会代理到外部,proxy: npmjs 指明了如果该包上传则会被代理到npm公有仓库,如果在下载某个不包含@xdh前缀的包时,会自动代理到npm公有仓库查找资源并下载,并且默认会将拉取的资源缓存到我们前面指定的 storage 文件夹中。

1.3.4 listen

设置内网可访问及端口号,内网可通过本机ip进行访问

listen:
	0.0.0.0: 3000
复制代码

设置完毕,重启verdaccio,即可访问使用。

二、pm2管理服务

为了能使服务后台运行,不受窗口关闭的影响,使用pm2进行服务管理

2.1 安装pm2

npm install pm2 -g
复制代码

2.2 运行verdaccio

直接运行pm2 start verdaccio,不能启动运行的话,使用如下方式启动

// 启动路径为全局node_modules下verdaccio\bin\verdaccio运行
pm2 start C:\Users\Administrator\AppData\Roaming\npm\node_modules\verdaccio\bin\verdaccio
复制代码

2.2 查看运行状态

查看运行状态status: online,即为正常启动,可以访问使用

pm2 list
复制代码

2.3 停止运行

pm2 stop verdaccio
复制代码

运行状态status: stoped

2.4 删除服务

pm2 delete verdaccio
复制代码

三、nrm管理npm源

一般在本地会安装多个npm源,可以使用nrm对其进行管理与切换。

3.1 安装

npm install nrm -g
复制代码

3.2 增加本地npm源

nrm add localnpm *.*.*.*:4873
复制代码

3.4 查看是否添加成功

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/
  localnpm --- http://*.*.*.*:4873/
复制代码

3.5 切换至本地源

nrm use localnpm

// 源列表
  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/
* localnpm --- http://*.*.*.*:4873/
复制代码

四、创建本地npm用户

需要创建本地npm用户,用于上传和管理npm包,使用npm adduser创建用户,输入用户名、密码、邮箱。

npm adduser
Username: yourname
Password: *
Email: *
复制代码

记录设置的用户信息,下一节上传会用到。

五、新建npm包并上传

新建@xdh/test项目,项目根目录新增index.js文件,npm 初始化项目,输入提示信息

npm init 
复制代码

上传至本地仓库,在项目根目录下运行以下命令:

// 登录npm 
// 根据提示输入账户/密码/邮箱
npm login

// 登录成功,发布
npm publish
复制代码

刷新本地链接,可以看到npm包已经发布成功。

查看本地磁盘仓库strage文件,是否已存储成功

六、安装本地依赖

确保当前npm源已切换到localnpm

npm install @xdh/test -S
复制代码

至此内网npm私服已经搭建完成,可以上传和使用私有包。

免责声明:文章版权归原作者所有,其内容与观点不代表Unitimes立场,亦不构成任何投资意见或建议。

程序员

472

相关文章推荐

未登录头像

暂无评论