1. 程式人生 > 實用技巧 >Electron:執行npm run build構建環境失敗解決方案

Electron:執行npm run build構建環境失敗解決方案

摘要

從GitLab上拉取專案程式碼後,執行命令npm run build構建electron打包環境時,由於某些原因(可能被某些牆阻止等),構建環境失敗,出現超時錯誤。錯誤程式碼如下:

錯誤程式碼段引用自:https://segmentfault.com/q/1010000022013746

• electron-builder  version=21.2.0 os=10.0.18363
  • loaded configuration  file=package.json ("build" field)
  • writing effective config  file=release\builder-effective-config.yaml
  • installing production dependencies  platform=win32 arch=x64 appDir=D:\jobs\electron\pc-chat\dist
  • packaging       platform=win32 arch=x64 electron=4.2.12 appOutDir=release\win-unpacked
  • downloading     url=https://github.com/electron-userland/electron-builder-binaries/releases/download/winCodeSign-2.5.0/winCodeSign-2.5.0.7z size=5.6 MB parts=1
  • retrying        attempt=1
  • retrying        attempt=2
  • retrying        attempt=3
  ⨯ part download request failed with status code 403
github.com/develar/app-builder/pkg/download.(*Part).doRequest
        /Volumes/data/Documents/app-builder/pkg/download/Part.go:126
github.com/develar/app-builder/pkg/download.(*Part).download
        /Volumes/data/Documents/app-builder/pkg/download/Part.go:67
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadResolved.func1.1
        /Volumes/data/Documents/app-builder/pkg/download/downloader.go:107
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.12.7/libexec/src/runtime/asm_amd64.s:1337
  ⨯ D:\jobs\electron\pc-chat\node_modules\[email protected]@app-builder-bin\win\x64\app-builder.exe exited with code ERR_ELECTRON_BUILDER_CANNOT_EXECUTE  stackTrace=

解決方案

為了更好更快的解決構建環境問題,需要我們手動下載好electron環境的壓縮資源包等,構建好的打包快取環境(存放手動下載資源包前,優先清除cache的快取記錄)。

方案具體步驟

    1. 清除electron和electron-builder的cache快取記錄(刪除資料夾中舊的快取資源包)。
    • eledtron快取資源地址:~/AppData/Local/electron/Cache

    • electron-builder快取資源地址:~/AppData/Local/electron-builder/Cache

    1. 根據錯誤提示一步一步的手動下載資源包(資源包版本直接下載當前提示的可用版本,具體類似下圖所示下載連結)。
  • 下載好快取資源包後,儲存到對應的cache檔案中。

    • electron檔案儲存方式:

    • electron-builder檔案儲存方式:

    • nsis檔案儲存方式:

    • winCodeSign儲存方式:

    1. 重新執行命令npm run build