Electron的npm包体积有点大,造成下载会遇到各种问题
yarn add electron --dev
1. yarn 安装后运行electron提示: Electron failed to install correctly
找到源码查看, \node_modules\electron\index.js
const pathFile = path.join(__dirname, 'path.txt');
function getElectronPath () {
let executablePath;
if (fs.existsSync(pathFile)) {
executablePath = fs.readFileSync(pathFile, 'utf-8');
}
if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {
return path.join(process.env.ELECTRON_OVERRIDE_DIST_PATH, executablePath || 'electron');
}
if (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'); }
}
2. 看源码主要是由于 path.txt 没有找到造成的
找到 package.json 看到启动脚本会执行 install.js
"scripts": {
"postinstall": "node install.js"
},
命令行当前目录,直接再次运行
node install.js
3. 此时会碰到 electron-v19.0.8-win32-x64.zip 下载中断的问题
>node install
Downloading electron-v19.0.8-win32-x64.zip: [====================----------------------------------------------------------------------------] 21% ETA: 5180.6 seconds
更换淘宝源 编辑 install.js,添加 mirrorOptions 项:
downloadArtifact({
version,
artifactName: 'electron',
force: process.env.force_no_cache === 'true',
cacheRoot: process.env.electron_config_cache,
checksums: process.env.electron_use_remote_checksums ? undefined : require('./checksums.json'),
platform,
arch,
mirrorOptions: {
mirror: 'https://npm.taobao.org/mirrors/electron/',
customDir: version,
},
}).then(extractFile).catch(err => {
console.error(err.stack);
process.exit(1);
});
再次执行顺利安装完成
回复 (0)
微信扫码 立即评论