electron-builder 打包失败原因之一invalid icon file

electron-builder打包失败原因非常多,本文介绍的原因是icon设置问题。

相关报错信息:

/electron-builder/nsis/nsis-3.0.4.1/mac/makensis process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE

/assets/logo.icns”: invalid icon file

Error in macro MUI_INTERFACE on macroline 87

Error in macro MUI_UNPAGE_INIT on macroline 7

Error in macro MUI_UNPAGE_WELCOME on macroline 5

!include: error in script: “assistedInstaller.nsh” on line 68

Error in script “<stdin>” on line 110 — aborting creation process

首先要明确各个平台的icon文件格式(建议):

WindowsmacOSLinux
.ico.icns.png

尺寸通常是256*256或者512*512

位置一:在package.json中,有三处icon设置。

// package.json

"build": {
...
    "nsis": {
      "oneClick": false,
      "guid": "Hello World",
      "perMachine": true,
      "allowElevation": true,
      "allowToChangeInstallationDirectory": true,
      "installerIcon": "./assets/logo.ico",
      "uninstallerIcon": "./assets/logo.ico",
      "installerHeaderIcon": "./assets/logo.ico",
      "createDesktopShortcut": true,
      "createStartMenuShortcut": true,
      "shortcutName": "Hello World"
    }
...
}

位置二:主程序main.js中创建BrowserWindow

  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    },
    icon: 'assets/logo.ico'
  })

(版权归cpury.com所有,转载请注明出处。)