问题描述
在构建一个mpvue项目的完毕之后,并且把依赖包都下载完成之后需要npm run dev运行项目,但是问题来了,既然卡死不动了。问题出现在mpvue小程序开发中。
场景复现
$ npm run dev
> mytools@1.0.0 dev O:\newproject\mytools
> node build/dev-server.js wx
到了这里就卡死不动了, ctrl+c强制停掉,在反复重启。哎多次重试之后又好了。下一在重新跑又不行,等了十来分钟还是卡死在这。没办法只能解决了。
思路
既然是nodejs的产物那么直接看他干嘛了,在哪里卡死了
- 找到指令 npm run dev的根源
- package.json文件中的指令
- “dev”: “node build/dev-server.js wx”,
- npm也就是执行了这条指令
- 开始用nodejs运行, 打断点
- 不归路开始…
原因
最后发现开始在了build文件夹下的check-versions.js, 来看下这个文件的源码
var chalk = require('chalk')
var semver = require('semver')
var packageConfig = require('../package.json')
var shell = require('shelljs')
function exec (cmd) {
return require('child_process').execSync(cmd).toString().trim()
}
var versionRequirements = [
{
name: 'node',
currentVersion: semver.clean(process.version),
versionRequirement: packageConfig.engines.node
}
]
if (shell.which('npm')) {
versionRequirements.push({
name: 'npm',
currentVersion: exec('npm --version'),
versionRequirement: packageConfig.engines.npm
})
}
module.exports = function () {
var warnings = []
for (var i = 0; i < versionRequirements.length; i++) {
var mod = versionRequirements[i]
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
warnings.push(mod.name + ': ' +
chalk.red(mod.currentVersion) + ' should be ' +
chalk.green(mod.versionRequirement)
)
}
}
if (warnings.length) {
console.log('')
console.log(chalk.yellow('To use this template, you must update following to modules:'))
console.log()
for (var i = 0; i < warnings.length; i++) {
var warning = warnings[i]
console.log(' ' + warning)
}
console.log()
process.exit(1)
}
}
卡在了这个函数这里
function exec (cmd) {
**return require('child_process').execSync(cmd).toString().trim()**
}
解决
这个是校验版本信息, 解决办法有两种。
- 一种是直接屏蔽掉,不管那么多我不需要校验版本, 听老夫的一把屏掉
- 第二种就是把npm改成node,代码如下
- 把npm–version修改成node–version
var versionRequirements = [
{
name: 'node',
currentVersion: semver.clean(process.version),
versionRequirement: packageConfig.engines.node
}
]
if (shell.which('node')) {
versionRequirements.push({
name: 'node',
currentVersion: exec('node--version'),
versionRequirement: packageConfig.engines.npm
})
}
重新运行 npm run dev
ojbk,解决问题
结束
入坑需谨慎!最后友情链接有我的博客和github地址,欢迎相互沟通学习!
想一起讨论/学习微信小游戏开发的,GO语言开发的,请微信搜索下方小程序加博主微信群

版权声明:本文为duqiming原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。