使用场景:
前端项目的编译运行开发中报下载经常出错:
项目场景:通常我们前端项目开发搭建过程中通过npm管理前端js库,新建项目或内网开发过程中经常出现环境搭建的问题,例如常见错误Failed at the chromedriver@2.37.0 install script 'node install.js'
,node-sass安装失败等,通过verdaccio搭建npm私有仓库,让前端开发环境搭建提速。
- 外网环境安装Verdaccio
- Verdaccio运行,添加项目及依赖包
- 拷贝npm文件到内网电脑
环境检查:
保证内网电脑与外网电脑的 nodejs版本一致,保证前端项目中使用的node-sass版本与nodejs兼容,nodeModule版本是 node-sass的项目模块
NodeJS | node-sass version | Node Module |
Node 14 | 4.14+ | 83 |
Node 13 | 4.13+ | 79 |
Node 12 | 4.12+ | 72 |
Node 11 | 4.10+ | 67 |
Node 10 | 4.9+ | 64 |
Node 8 | 4.5.3+ | 57 |
关于Node版本:
根据我们项目中依赖node-sass的版本,我们选中向下兼容安装nodejs.根据项目的node-sass,
"dependencies": {
"abab": "^1.0.4",
"axios": "0.17.1",
"babel-plugin-component": "0.10.1",
"babel-polyfill": "6.26.0",
"chromedriver": "^89.0.0",
"element-ui": "2.8.2",
"emoji-regex": "^7.0.1",
"gulp": "4.0.2",
"gulp-concat": "2.6.1",
"gulp-load-plugins": "2.0.5",
"gulp-replace": "1.0.0",
"gulp-shell": "0.8.0",
"lodash": "4.17.5",
"node-sass": "^4.13.1",
"npm": "^6.9.0",
"sass-loader": "6.0.6",
"strip-bom": "^3.0.0",
"svg-sprite-loader": "3.7.3",
"vue": "2.5.16",
"vue-cookie": "1.1.4",
"vue-router": "3.0.1",
"vuex": "3.0.1"
},
我机器上安装的node 是
$ node -v
v13.14.0
配置文件位置:
外网电脑,verdaccio安装,启动:
安装,在Git Bash中运行命令
npm install --global verdaccio
启动,在命令行运行, verdaccio,启动成功可通过 http://127.0.0.1:4873/ ,访问Verdaccio界面, 其中打印信息中有 配置文件位置:C:\Users\lch.config\verdaccio\config.yaml,
lch@desktop-n6td65m MINGW64 ~/Desktop
$ verdaccio
warn --- config file - C:\Users\lch\.config\verdaccio\config.yaml
warn --- Verdaccio started
warn --- Plugin successfully loaded: verdaccio-htpasswd
warn --- Plugin successfully loaded: verdaccio-audit
warn --- http address - http://0.0.0.0:4873/ - verdaccio/4.12.0
发布项目到verdaccio:
当前npm 服务指向 本地
npm set registry http://localhost:4873
注册用户
npm adduser –registry http://localhost:4873
按照提示录入用户名密码
发布项目到Verdaccio
到开发的项目根目录下即package.json的目录,执行命令:
npm publish
发布好项目了
安装项目的依赖到verdaccio
删除项目的node_modules文件夹,执行命令:
npm install --registry http://localhost:4873
首先会去 本地的 verdaccio下载依赖,如果本地没有会去 uplinks 配置的公有仓库下载,并缓存到 storage 文件夹下。根据配置文件的位置,我们找到storage的文件夹位置:C:\Users\lch.config\verdaccio\storage
这个过程应该是异步过程,等安装完成后,需要等一会verdaccio缓存才会完成。
部署到内网(离线环境):
内网安装好 nodejs(与外网版本保持一致) 和 git bash后,
我们拷贝 npm全局目录与缓存目录及verdaccio的依赖库目录到覆盖内网的对应目录中。
覆盖npm全局目录及缓存目录
npm全局目录及缓存目录默认在:
C:\Users\用户名\AppData\Roaming\npm
用户名为当前登录用户,也可能是在
C:\Users\Administrator\AppData\Roaming\npm
如果你修改了全局目录和缓存目录,使用命令
npm config ls
查看全局目录位置
prefix = "C:\Users\lch\AppData\Roaming\npm"
覆盖verdaccio缓存目录
verdaccio的安装文件已经在npm的全局目录中了,我们还要复制一份缓存目录到内网机器用户的.config目录下
外网目录:C:\Users\lch\.config\verdaccio
内网目录:C:\Users\用户名\.config\verdaccio
离线环境运行
同外网,启动verdaccio 启动后访问:
如果内网环境中没有发布项目了,说明配置文件可能不对,将npm全局目录下的
default.yaml文件内容替换成 verdaccio 缓存目录下的 config.yaml 内容
\npm\node_modules\verdaccio\conf\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
storage: C:\Users\lch\.config\verdaccio\storage
# path to a directory with plugins to include
#plugins: ./plugins
plugins: C:\Users\lch\.config\verdaccio\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
# 监听的端口 ,重点, 不配置这个,只能本机能访问
listen: 0.0.0.0:4873
# 允许离线发布
publish:
allow_offline: 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