Electron-Builder(404)错误

弓明亮
2023-12-01

错误来源及分析

错误提示内容:

cannot resolve http://npm.taobao.org/mirrors/electron/15.2.0/electron-v15.2.0-win32-x64.zip: status code 404

  ⨯ cannot resolve http://npm.taobao.org/mirrors/electron/15.2.0/electron-v15.2.0-win32-x64.zip: status code 404
github.com/develar/app-builder/pkg/download.(*Downloader).follow
        /Volumes/data/Documents/app-builder/pkg/download/downloader.go:237
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadNoRetry
        /Volumes/data/Documents/app-builder/pkg/download/downloader.go:128
github.com/develar/app-builder/pkg/download.(*Downloader).Download
        /Volumes/data/Documents/app-builder/pkg/download/downloader.go:112
github.com/develar/app-builder/pkg/electron.(*ElectronDownloader).doDownload
        /Volumes/data/Documents/app-builder/pkg/electron/electronDownloader.go:192
github.com/develar/app-builder/pkg/electron.(*ElectronDownloader).Download
        /Volumes/data/Documents/app-builder/pkg/electron/electronDownloader.go:177
github.com/develar/app-builder/pkg/electron.downloadElectron.func1.1
        /Volumes/data/Documents/app-builder/pkg/electron/electronDownloader.go:73
github.com/develar/app-builder/pkg/util.MapAsyncConcurrency.func2
        /Volumes/data/Documents/app-builder/pkg/util/async.go:68
runtime.goexit
        /usr/local/Cellar/go/1.16.5/libexec/src/runtime/asm_amd64.s:1371  
  ⨯ D:\CODE\Electron\newplayer\node_modules\app-builder-bin\win\x64\app-builder.exe exited with code ERR_ELECTRON_BUILDER_CANNOT_EXECUTE  failedTask=build stackTrace=Error: D:\CODE\Electron\newplayer\node_modules\app-builder-bin\win\x64\app-builder.exe exited with code ERR_ELECTRON_BUILDER_CANNOT_EXECUTE
    at ChildProcess.<anonymous> (D:\CODE\Electron\newplayer\node_modules\builder-util\src\util.ts:250:14)
    at Object.onceWrapper (events.js:482:26)
    at ChildProcess.emit (events.js:375:28)
    at ChildProcess.cp.emit (D:\CODE\Electron\newplayer\node_modules\cross-spawn\lib\enoent.js:34:29)
    at maybeClose (internal/child_process.js:1055:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

问题细述:

在下载build包阶段,出现该404错误,详细对比了准确的下载地址发现,地址中少了一个v,即

http://npm.taobao.org/mirrors/electron/v15.2.0/electron-v15.2.0-win32-x64.zip,对比github上的源库发现同样带有v,不知道什么时候改的,直接导致我们请求不到正确的地址。

问题解决过程细述

那么问题就很简单了,就是要使得这个地址加上这个v,或者可以写死,或者可以跳过下载阶段。(在github上的源库发现其同样需要在中间这个文件夹加上v,才能成为有效路径)。地址的获取通过electron-builder包发现,其是将config文件中的内容传递给了@electron/get库,由这个库获取下载地址,根据@electron/get的描述,他将地址分为如下三个部分:

https://github.com/electron/electron/releases/download/v4.0.4/electron-v4.0.4-linux-x64.zip
|                                                     |       |                           |
-------------------------------------------------------       -----------------------------
                        |                                                   |
              mirror / nightlyMirror                  |    |         customFilename
                                                       ------
                                                         ||
                                                      customDir

方案1:缓存,在electron-builder.config.js文件中写明缓存地址,然后将下载的electron版本文件放入其中

    electronDownload: {
        cache: '%LOCALAPPDATA%/electron/Cache',
    },

产生结果:失败。追踪electron目录的install.js文件的源码发现,force_no_cache被默认设置为true,就是不使用缓存,因此并未生效。

方案2:尝试在代码中加入补全这个v,发现在@electron/get库中对于中间字段进行了如下处理

    const path = mirrorVar('customDir', opts, details.version).replace('{{ version }}', details.version.replace(/^v/, ''));

直接修改代码(删去replace后)后发现并未生效,可能还需要其他设置或者另一部位修改才行。

方案3:写死中间这个文件路径

观察electron-builder发现其electronDownload配置项是直接传递给@electron/get选项的,因此可以直接在customDir属性中,将其写死,设置为我们想要的版本,如我的库中,electron版本为15.1.2,因此我直接将该属性配置为如下所示,报错消失。问题部分解决。

    electronDownload: {
        cache: '%LOCALAPPDATA%/electron/Cache',
        mirror: 'https://npm.taobao.org/mirrors/electron/',
        customDir: 'v15.1.2',
    }

方案4:更新node_nodules中的@electron/get库

 类似资料: