由于公司为保密部门,与外网隔离,则搭建内网npm包管理势在必行,查阅资料后决定使用verdaccio
、pm2
、nrm
等工具完成。
verdaccio
verdaccio是什么?
verdaccio是一种绿色,直观上看去偏墨绿,也可能是作者喜欢,来自意大利。😂
verdaccio 是一个 node.js创建的轻量、私有的npm proxy registry
官网总结:
- 一个node.js的网页应用程序
- 私有化npm源
- 本地网络的proxy
Registry是什么?
- 源(Registry)就是管理包的储存库(package),实现了储存库CommandJS兼容包的源规范。—
package.json
里面的定义。 - 提供包管理工具的接口,如
npm
、yarn
、pnpm
。—可以通过包管理工具安装 - 提供语意化的版本(兼容semver)。 —可以查看版本
安装
配置要求
- 环境:
node.js@8.x +
- 包管理:
npm@6.x+
、pnpm@5.x
、yarn@2.x
- 浏览器: 现代浏览器or
>= IE.11
因为不太喜欢yarn后面的步骤都是通过npm完成
install via CLI
win: npm install -g verdaccio
mac: sudo npm install -g verdaccio
查看版本
verdaccio -V
运行
verdaccio
配置
找到其中的config file
打开,config是verdaccio程序的基础,我们修改特性、启用插件、扩展特性都是在这里配置,这个文件是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: /Users/pheeno/.local/share/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
# 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
storage
存放npm包的目录,单独建一个专门用来存储
# path to a directory with all packages
storage:D:/public_repo
plugins
插件的包目录,对基于Docker
、Kubernetes
部署的项目需要用到
web
用于显示源的网页,其中的配置
enable: true 是否开启网页
title: ‘xxxxx’ 网页头部显示标题
logo: ./xxx/xxx/xxx.png 网页中显示的logo图标
auth
uplinks
本地npm包如果找不到,会默认从其他的源中拉取,设置成npm官方源
# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages
server
middlewares
logs
局域网启动
- 安装
verdaccio
由于win10在node_modules
下安装服务失败,不明原因,则在其他盘单独安装verdaccio
,这里我是安装到了事先准备好的用于做storage
的文件中,假设是E://public_repo
然后运行npm install verdaccio
- 本地开启服务
nssm
(windows)
发布源
Tips&Issuses
- 离线状态下,局域网访问源,一直返回
500 error:uplink offline
解决: config.yaml
中加入publish: allow_offline: true
nrm
nrm
是一个用来切换npm
源(registry)数据的工具,配合verdaccio
使用很方便。
安装
npm install -g nrm
使用
//查看所有源
nrm ls
//使用某个源
nrm use taobao
//添加源
nrm add my-registry http://my-location.com
//删除源
nrm del my-registry
Tips & Issuses
在node低版本,win7系统中会出现 internal/validator.js
这个文件报错,
解决:首先查看npm config
配置npm config ls
如果是win32
则进入报错的文件
将const NRMRC = path.join(process.env.HOME, '.nrmrc');
改为const NRMRC = path.join(process.env.USERPROFILE, '.nrmrc');