内网代理上网环境下,搭建vue-element-admin环境
公司(外包)开发机器一直只能通过http代理上网,我觉得领导们给这个小水管只是为了方便查查资料。
但是作为面向网络开发的码农,下载github开源项目、下载maven依赖包等都是需要网络。git下载通过配置git的http代理就可以实现。maven下载jar包因为网络问题总是超时,为此我还特地本机搭建了一个nexus的maven私有仓库。
最近项目组准备重构十几年前的老项目,甲方提供了一个nexus私有仓库(终于不用使用自己的小水管下jar包了)。甲方准备使用vue构建前端项目。但是实际使用发现,网络环境是很大的障碍。这就产生了本篇笔记了。如何在内网环境下搭建vue(本篇特质vue-element-admin)的开发环境。
初始想法
因为某种原因,国内访问npm就是很慢。所以大家一般都会使用淘宝镜像cnpm。这里就不详细写如何配置了,建议通过alias的方式使用,不要直接安装cnpm,据说会有问题。本机因为网络太差,直接使用npm config set registry https://registry.npm.taobao.org
设置全局仓库地址。
当然配置淘宝镜像还不行,因为本机是使用http代理上网,所以还要配置npm的http代理。
#设置网络代理的命令
npm config set proxy="http://192.168.2.1:8080"
一切准备就绪,运行npm install时发现,理论可行,实际因为网速问题,无法运行。只能另寻他法。
使用私有仓库
因为甲方提供了一个nexus私有仓库。之前有了解到,nexus不仅能作为maven仓库,也可以做npm代理仓库。登陆私有仓库管理页面找到名字叫npm-group的仓库,复制仓库地址。
执行命令npm config set registry http://192.168.2.1/nexus/repository/npm-group/
使用私有仓库镜像。(因为私有仓库在内网,需要取消npm的网络代理npm config delete proxy
)
git问题
以为这就完了?还没有!
在vue-element-admin项目目录执行npm install
发现速度确实很快。但是会报错!
24294 verbose stack Error: exited with error code: 128
24294 verbose stack at ChildProcess.<anonymous> (D:Programs
ode-v10.16.3-win-x64
ode-global
ode_modules
pm
ode_modulespacotelibutilfinished.js:12:19)
24294 verbose stack at emitTwo (events.js:126:13)
24294 verbose stack at ChildProcess.emit (events.js:214:7)
24294 verbose stack at maybeClose (internal/child_process.js:925:16)
24294 verbose stack at Socket.stream.socket.on (internal/child_process.js:346:11)
24294 verbose stack at emitOne (events.js:116:13)
24294 verbose stack at Socket.emit (events.js:211:7)
24294 verbose stack at Pipe._handle.close [as _onclose] (net.js:557:12)
24295 verbose cwd E:dzgvue-element-admin
24296 verbose Windows_NT 6.1.7601
24297 verbose argv "C:\Program Files\nodejs\node.exe" "D:\Programs\node-v10.16.3-win-x64\node-global\node_modules\npm\bin\npm-cli.js" "install"
24298 verbose node v8.11.3
24299 verbose npm v6.14.8
24300 error Error while executing:
24300 error D:ProgramsGitingit.EXE ls-remote -h -t git://github.com/adobe-webplatform/eve.git
24300 error
24300 error fatal: unable to connect to github.com:
24300 error github.com[0: 52.74.223.119]: errno=No error
24300 error
24300 error
24300 error exited with error code: 128
24301 verbose exit [ 1, true ]
看提示内容,是从github拉取时网络超时。因为本机已经给git配置了代理。应该可以正常拉取的。
问题在于这个拉取的地址是git://
开头,看来git配置的http代理无法代理git://
协议。
这也是可以解决的。一般情况下,github公开的仓库拉取时可以使用https协议。如果我将拉取的地址改成https协议的是不是就可以正常拉取了呢?
那就要找到调用git命令的地方,修改参数。控制台日志显示报错的地方是D:Programs
ode-v10.16.3-win-x64
ode-global
ode_modules
pm
ode_modulespacotelibutilfinished.js:12:19
。
打开文件如下。
'use strict'
const BB = require('bluebird')
module.exports = function (child, hasExitCode = false) {
return BB.fromNode(function (cb) {
child.on('error', cb)
child.on(hasExitCode ? 'close' : 'end', function (exitCode) {
if (exitCode === undefined || exitCode === 0) {
cb()
} else {
let err = new Error('exited with error code: ' + exitCode)
cb(err)
}
})
})
}
好像没有啥有用信息。不过在这个文件所在的文件夹里面,发现了一个git.js的文件。
打开一看发现里面封装了对git的操作。搜索ls-remote
关键字。
找到代码块:
function revs (repo, opts) {
opts = optCheck(opts)
const cached = REVS.get(repo)
if (cached) {
return BB.resolve(cached)
}
return pinflight(`ls-remote:${repo}`, () => {
return spawnGit(['ls-remote', '-h', '-t', repo], {
env: gitEnv()
}, opts).then((stdout) => {
return stdout.split('
').reduce((revs, line) => {
const split = line.split(/s+/, 2)
if (split.length < 2) { return revs }
const sha = split[0].trim()
const ref = split[1].trim().match(/(?:refs/[^/]+/)?(.*)/)[1]
if (!ref) { return revs } // ???
if (ref.endsWith(CARET_BRACES)) { return revs } // refs/tags/x^{} crap
const type = refType(line)
const doc = { sha, ref, type }
revs.refs[ref] = doc
// We can check out shallow clones on specific SHAs if we have a ref
if (revs.shas[sha]) {
revs.shas[sha].push(ref)
} else {
revs.shas[sha] = [ref]
}
if (type === 'tag') {
const match = ref.match(/v?(d+.d+.d+(?:[-+].+)?)$/)
if (match && semver.valid(match[1], true)) {
revs.versions[semver.clean(match[1], true)] = doc
}
}
return revs
}, { versions: {}, 'dist-tags': {}, refs: {}, shas: {} })
}, err => {
err.message = `Error while executing:
${GITPATH} ls-remote -h -t ${repo}
${err.stderr}
${err.message}`
throw err
}).then(revs => {
if (revs.refs.HEAD) {
const HEAD = revs.refs.HEAD
Object.keys(revs.versions).forEach(v => {
if (v.sha === HEAD.sha) {
revs['dist-tags'].HEAD = v
if (!revs.refs.latest) {
revs['dist-tags'].latest = revs.refs.HEAD
}
}
})
}
REVS.set(repo, revs)
return revs
})
})
}
猜测第一个参数repo应该是仓库地址。在函数第一行加上repo = repo.replace('git://','https://');
。保存,再次执行npm install
。
23712 verbose stack Error: Command failed: git clone --mirror -q git://github.com/adobe-webplatform/eve.git D:Programs
ode-v10.16.3-win-x64
ode-cache\_cacache mpgit-clone-8ab7d4f1.git --config core.longpaths=true
23712 verbose stack warning: templates not found in C:UsersXC85E2~1.DINAppDataLocalTemppacote-git-template-tmpgit-clone-cf99ec69
23712 verbose stack fatal: unable to connect to github.com:
23712 verbose stack github.com[0: 52.74.223.119]: errno=No error
23712 verbose stack
23712 verbose stack
23712 verbose stack at ChildProcess.exithandler (child_process.js:275:12)
23712 verbose stack at emitTwo (events.js:126:13)
23712 verbose stack at ChildProcess.emit (events.js:214:7)
23712 verbose stack at maybeClose (internal/child_process.js:925:16)
23712 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
23713 verbose cwd E:dzgvue-element-admin
23714 verbose Windows_NT 6.1.7601
23715 verbose argv "C:\Program Files\nodejs\node.exe" "D:\Programs\node-v10.16.3-win-x64\node-global\node_modules\npm\bin\npm-cli.js" "install"
23716 verbose node v8.11.3
23717 verbose npm v6.14.8
23718 error code 128
23719 error Command failed: git clone --mirror -q git://github.com/adobe-webplatform/eve.git D:Programs
ode-v10.16.3-win-x64
ode-cache\_cacache mpgit-clone-8ab7d4f1.git --config core.longpaths=true
23719 error warning: templates not found in C:UsersXC85E2~1.DINAppDataLocalTemppacote-git-template-tmpgit-clone-cf99ec69
23719 error fatal: unable to connect to github.com:
23719 error github.com[0: 52.74.223.119]: errno=No error
23720 verbose exit [ 1, true ]
还是报错,不过和前面的错不一样。看来这么改没问题,只是还有地方没改到。最后查看git.js中其他的git命令。按照前面的方法调整。
调整后的git.js
'use strict'
const BB = require('bluebird')
const cp = require('child_process')
const execFileAsync = BB.promisify(cp.execFile, {
multiArgs: true
})
const finished = require('./finished')
const LRU = require('lru-cache')
const optCheck = require('./opt-check')
const osenv = require('osenv')
const path = require('path')
const pinflight = require('promise-inflight')
const promiseRetry = require('promise-retry')
const uniqueFilename = require('unique-filename')
const which = BB.promisify(require('which'))
const semver = require('semver')
const inferOwner = require('infer-owner')
const GOOD_ENV_VARS = new Set([
'GIT_ASKPASS',
'GIT_EXEC_PATH',
'GIT_PROXY_COMMAND',
'GIT_SSH',
'GIT_SSH_COMMAND',
'GIT_SSL_CAINFO',
'GIT_SSL_NO_VERIFY'
])
const GIT_TRANSIENT_ERRORS = [
'remote error: Internal Server Error',
'The remote end hung up unexpectedly',
'Connection timed out',
'Operation timed out',
'Failed to connect to .* Timed out',
'Connection reset by peer',
'SSL_ERROR_SYSCALL',
'The requested URL returned error: 503'
].join('|')
const GIT_TRANSIENT_ERROR_RE = new RegExp(GIT_TRANSIENT_ERRORS)
const GIT_TRANSIENT_ERROR_MAX_RETRY_NUMBER = 3
function shouldRetry (error, number) {
return GIT_TRANSIENT_ERROR_RE.test(error) && (number < GIT_TRANSIENT_ERROR_MAX_RETRY_NUMBER)
}
const GIT_ = 'GIT_'
let GITENV
function gitEnv () {
if (GITENV) { return GITENV }
const tmpDir = path.join(osenv.tmpdir(), 'pacote-git-template-tmp')
const tmpName = uniqueFilename(tmpDir, 'git-clone')
GITENV = {
GIT_ASKPASS: 'echo',
GIT_TEMPLATE_DIR: tmpName
}
Object.keys(process.env).forEach(k => {
if (GOOD_ENV_VARS.has(k) || !k.startsWith(GIT_)) {
GITENV[k] = process.env[k]
}
})
return GITENV
}
let GITPATH
try {
GITPATH = which.sync('git')
} catch (e) {}
module.exports.clone = fullClone
function fullClone (repo, committish, target, opts) {
repo = repo.replace('git://','https://');
opts = optCheck(opts)
console.log(target)
const gitArgs = ['clone', '--mirror', '-q', repo, path.join(target, '.git')]
if (process.platform === 'win32') {
gitArgs.push('--config', 'core.longpaths=true')
}
return execGit(gitArgs, { cwd: target }, opts).then(() => {
return execGit(['init'], { cwd: target }, opts)
}).then(() => {
return execGit(['checkout', committish || 'HEAD'], { cwd: target }, opts)
}).then(() => {
return updateSubmodules(target, opts)
}).then(() => headSha(target, opts))
}
module.exports.shallow = shallowClone
function shallowClone (repo, branch, target, opts) {
repo = repo.replace('git://','https://');
opts = optCheck(opts)
const gitArgs = ['clone', '--depth=1', '-q']
if (branch) {
gitArgs.push('-b', branch)
}
gitArgs.push(repo, target)
if (process.platform === 'win32') {
gitArgs.push('--config', 'core.longpaths=true')
}
return execGit(gitArgs, {
cwd: target
}, opts).then(() => {
return updateSubmodules(target, opts)
}).then(() => headSha(target, opts))
}
function updateSubmodules (localRepo, opts) {
const gitArgs = ['submodule', 'update', '-q', '--init', '--recursive']
return execGit(gitArgs, {
cwd: localRepo
}, opts)
}
function headSha (repo, opts) {
opts = optCheck(opts)
return execGit(['rev-parse', '--revs-only', 'HEAD'], { cwd: repo }, opts).spread(stdout => {
return stdout.trim()
})
}
const CARET_BRACES = '^{}'
const REVS = new LRU({
max: 100,
maxAge: 5 * 60 * 1000
})
module.exports.revs = revs
function revs (repo, opts) {
repo = repo.replace('git://','https://');
opts = optCheck(opts)
const cached = REVS.get(repo)
if (cached) {
return BB.resolve(cached)
}
return pinflight(`ls-remote:${repo}`, () => {
return spawnGit(['ls-remote', '-h', '-t', repo], {
env: gitEnv()
}, opts).then((stdout) => {
return stdout.split('
').reduce((revs, line) => {
const split = line.split(/s+/, 2)
if (split.length < 2) { return revs }
const sha = split[0].trim()
const ref = split[1].trim().match(/(?:refs/[^/]+/)?(.*)/)[1]
if (!ref) { return revs } // ???
if (ref.endsWith(CARET_BRACES)) { return revs } // refs/tags/x^{} crap
const type = refType(line)
const doc = { sha, ref, type }
revs.refs[ref] = doc
// We can check out shallow clones on specific SHAs if we have a ref
if (revs.shas[sha]) {
revs.shas[sha].push(ref)
} else {
revs.shas[sha] = [ref]
}
if (type === 'tag') {
const match = ref.match(/v?(d+.d+.d+(?:[-+].+)?)$/)
if (match && semver.valid(match[1], true)) {
revs.versions[semver.clean(match[1], true)] = doc
}
}
return revs
}, { versions: {}, 'dist-tags': {}, refs: {}, shas: {} })
}, err => {
err.message = `Error while executing:
${GITPATH} ls-remote -h -t ${repo}
${err.stderr}
${err.message}`
throw err
}).then(revs => {
if (revs.refs.HEAD) {
const HEAD = revs.refs.HEAD
Object.keys(revs.versions).forEach(v => {
if (v.sha === HEAD.sha) {
revs['dist-tags'].HEAD = v
if (!revs.refs.latest) {
revs['dist-tags'].latest = revs.refs.HEAD
}
}
})
}
REVS.set(repo, revs)
return revs
})
})
}
// infer the owner from the cwd git is operating in, if not the
// process cwd, but only if we're root.
// See: https://github.com/npm/cli/issues/624
module.exports._cwdOwner = cwdOwner
function cwdOwner (gitOpts, opts) {
const isRoot = process.getuid && process.getuid() === 0
if (!isRoot || !gitOpts.cwd) { return Promise.resolve() }
return BB.resolve(inferOwner(gitOpts.cwd).then(owner => {
gitOpts.uid = owner.uid
gitOpts.gid = owner.gid
}))
}
module.exports._exec = execGit
function execGit (gitArgs, gitOpts, opts) {
opts = optCheck(opts)
return BB.resolve(cwdOwner(gitOpts, opts).then(() => checkGit(opts).then(gitPath => {
return promiseRetry((retry, number) => {
if (number !== 1) {
opts.log.silly('pacote', 'Retrying git command: ' + gitArgs.join(' ') + ' attempt # ' + number)
}
return execFileAsync(gitPath, gitArgs, mkOpts(gitOpts, opts)).catch((err) => {
if (shouldRetry(err, number)) {
retry(err)
} else {
throw err
}
})
}, opts.retry != null ? opts.retry : {
retries: opts['fetch-retries'],
factor: opts['fetch-retry-factor'],
maxTimeout: opts['fetch-retry-maxtimeout'],
minTimeout: opts['fetch-retry-mintimeout']
})
})))
}
module.exports._spawn = spawnGit
function spawnGit (gitArgs, gitOpts, opts) {
opts = optCheck(opts)
return BB.resolve(cwdOwner(gitOpts, opts).then(() => checkGit(opts).then(gitPath => {
return promiseRetry((retry, number) => {
if (number !== 1) {
opts.log.silly('pacote', 'Retrying git command: ' + gitArgs.join(' ') + ' attempt # ' + number)
}
const child = cp.spawn(gitPath, gitArgs, mkOpts(gitOpts, opts))
let stdout = ''
let stderr = ''
child.stdout.on('data', d => { stdout += d })
child.stderr.on('data', d => { stderr += d })
return finished(child, true).catch(err => {
if (shouldRetry(stderr, number)) {
retry(err)
} else {
err.stderr = stderr
throw err
}
}).then(() => {
return stdout
})
}, opts.retry)
})))
}
module.exports._mkOpts = mkOpts
function mkOpts (_gitOpts, opts) {
const gitOpts = {
env: gitEnv()
}
const isRoot = process.getuid && process.getuid() === 0
// don't change child process uid/gid if not root
if (+opts.uid && !isNaN(opts.uid) && isRoot) {
gitOpts.uid = +opts.uid
}
if (+opts.gid && !isNaN(opts.gid) && isRoot) {
gitOpts.gid = +opts.gid
}
Object.assign(gitOpts, _gitOpts)
return gitOpts
}
function checkGit (opts) {
if (opts.git) {
return BB.resolve(opts.git)
} else if (!GITPATH) {
const err = new Error('No git binary found in $PATH')
err.code = 'ENOGIT'
return BB.reject(err)
} else {
return BB.resolve(GITPATH)
}
}
const REFS_TAGS = 'refs/tags/'
const REFS_HEADS = 'refs/heads/'
const HEAD = 'HEAD'
function refType (ref) {
return ref.indexOf(REFS_TAGS) !== -1
? 'tag'
: ref.indexOf(REFS_HEADS) !== -1
? 'branch'
: ref.endsWith(HEAD)
? 'head'
: 'other'
}
再次执行npm install
。这下成功结束。可以愉快的npm run dev
了。
结语
本人是java开发,习惯了maven的简单和方便。对npm这种配置了仓库还要从git获取信息的方式很不理解。或许是为了更加灵活吧?
最后,希望每个遇到问题的人都能愿意了解其原理,找到解决方法。