问题
当安装了Electron 8.2.0 到项目中准备打包时,错误提示
> your-app@1.0.0 start D:\electron\demo
> electron .
D:\electron\demo\node_modules\electron\index.js:14
throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')
^
Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
at getElectronPath (D:\electron\demo\node_modules\electron\index.js:14:11)
at Object.<anonymous> (D:\electron\demo\node_modules\electron\index.js:18:18)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (D:\electron\demo\node_modules\electron\cli.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:774:30)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! your-app@1.0.0 start: `electron .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the your-app@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\admin\AppData\Roaming\npm-cache\_logs\2019-12-27T09_00_14_091Z-debug.log
于是打开报错代码
var fs = require('fs')
var path = require('path')
var pathFile = path.join(__dirname, 'path.txt')
function getElectronPath () {
if (fs.existsSync(pathFile)) {
var executablePath = fs.readFileSync(pathFile, 'utf-8')
if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {
return path.join(process.env.ELECTRON_OVERRIDE_DIST_PATH, executablePath)
}
return path.join(__dirname, 'dist', executablePath)
} else {
throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')
}
}
module.exports = getElectronPath()
可以看出是因为没有找到path.txt文件,我看了下目录,果然没有这个文件,那这个文件是本来就有的么,我去github查了一下,github上也没有啊,在源码搜了一下,发现在install.js中有创建这个文件,那这个install.js是什么时候运行的呢,我们来看下package.json,发现script中有个postinstall命令,这个命令是指装完包运行,所以是装完包运行这个命令报错了?
我们进这个目录运行下命令yarn run postinstall,下载失败,原因是:使用了淘宝源,淘宝源版本号没有v,找不到对应资源。
异常地址:
https://npm.taobao.org/mirrors/electron/v8.2.0/
成功地址:
https://npm.taobao.org/mirrors/electron/8.2.0/
解决
electron 官方提供了修改目录名称的环境变量electron_custom_dir
yarn config set electron_custom_dir 8.2.0
再运行yarn run postinstall,嗯,很快就成功了