跳到内容

NSIS

顶层 nsis 键包含一组选项,用于指示 electron-builder 如何构建 NSIS 目标(Windows 的默认目标)。

这些选项也适用于 Web 安装程序,请使用顶层 nsisWeb 键。


默认启用 Unicode。支持长字符串(最大字符串长度为 8192 字节,而不是默认的 1024 字节)。

32 位 + 64 位

如果您构建 ia32 和 x64 架构 (--x64 --ia32),您在任何情况下都会得到一个安装程序。将自动安装适当的架构。这同样适用于 Web 安装程序 (nsis-web target)。

Web 安装程序

要构建 Web 安装程序,请将 target 设置为 nsis-web。Web 安装程序会自动检测操作系统架构并下载相应的软件包文件。因此,用户无需猜测要下载哪个安装程序,同时您也不需要在同一个安装程序中捆绑所有架构的软件包文件(就像默认的 nsis 目标一样)。对于常见的 Electron 应用程序来说这无关紧要(由于出色的 LZMA 压缩,尺寸差异是可以接受的),但如果您的应用程序非常庞大,Web 安装程序是一个解决方案。

要自定义 Web 安装程序,请使用顶层 nsisWeb 键(而不是 nsis)。

如果由于某些原因 Web 安装程序无法下载(防病毒软件,离线)

  • 将软件包文件下载到安装程序所在的同一目录中。它将被自动检测到并代替从互联网下载使用。请注意 - 仅允许使用原始软件包文件(已检查校验和)。
  • 使用 --package-file=path_to_file 指定任何本地软件包文件。

自定义 NSIS 脚本

有两个选项可用 - includescriptscript 允许您提供完全不同的 NSIS 脚本。对于大多数情况,这并非必需,因为您只需要自定义某些方面,但仍然使用经过良好测试和维护的默认 NSIS 脚本。因此,建议使用 include

请记住 - 如果您自定义 NSIS 脚本,您应始终在问题报告中说明这一点。并且不要期望您的问题会得到解决。

  1. 添加文件 build/installer.nsh
  2. 定义要自定义的宏:customHeaderpreInitcustomInitcustomUnInitcustomInstallcustomUnInstallcustomRemoveFilescustomInstallModecustomWelcomePagecustomUnWelcomePage

    示例

    !macro customHeader
      !system "echo '' > ${BUILD_RESOURCES_DIR}/customHeader"
    !macroend
    
    !macro preInit
      ; This macro is inserted at the beginning of the NSIS .OnInit callback
      !system "echo '' > ${BUILD_RESOURCES_DIR}/preInit"
    !macroend
    
    !macro customInit
      !system "echo '' > ${BUILD_RESOURCES_DIR}/customInit"
    !macroend
    
    !macro customInstall
      !system "echo '' > ${BUILD_RESOURCES_DIR}/customInstall"
    !macroend
    
    !macro customInstallMode
      # set $isForceMachineInstall or $isForceCurrentInstall
      # to enforce one or the other modes.
    !macroend
    
    !macro customWelcomePage
      # Welcome Page is not added by default for installer.
      !insertMacro MUI_PAGE_WELCOME
    !macroend
    
    !macro customUnWelcomePage
      !define MUI_WELCOMEPAGE_TITLE "custom title for uninstaller welcome page"
      !define MUI_WELCOMEPAGE_TEXT "custom text for uninstaller welcome page $\r$\n more"
      !insertmacro MUI_UNPAGE_WELCOME
    !macroend
    
  3. BUILD_RESOURCES_DIRPROJECT_DIR 已定义。

  4. build 已添加为 addincludedir(即,您无需使用 BUILD_RESOURCES_DIR 来包含文件)。
  5. build/x86-unicodebuild/x86-ansi 已添加为 addplugindir
  6. 文件关联宏 registerFileAssociationsunregisterFileAssociations 仍然被定义。
  7. 所有其他 electron-builder 特定的标志(例如 ONE_CLICK)仍然被定义。

如果您想包含在安装期间使用的其他资源,例如脚本或其他安装程序,您可以将它们放在 build 目录中,并使用 File 包含它们。例如,要在安装期间包含并运行 extramsi.msi,请将其放在 build 目录中,并使用以下代码

!macro customInstall
  File /oname=$PLUGINSDIR\extramsi.msi "${BUILD_RESOURCES_DIR}\extramsi.msi"
  ExecWait '"msiexec" /i "$PLUGINSDIR\extramsi.msi" /passive'
!macroend
是否有一种方法仅在手动安装(或卸载)应用程序时调用,而不是在更新时调用?

使用 ${isUpdated}

${ifNot} ${isUpdated}
  # your code
${endIf}

GUID vs 应用程序名称

Windows 要求使用注册表项(例如 INSTALL/UNINSTALL 信息)。Squirrel.Windows 只是简单地使用应用程序名称作为键。但这并不稳健 - Google 可以使用键 Google Chrome SxS,因为它是一个 Google 产品。

因此,最好使用 GUID。您不必显式指定它 - 基于名称的 UUID v5 将从您的 appIdname 生成。这意味着一旦您的应用程序投入使用,您**不应更改 appId**(如果未设置 appId,则不应更改名称)。应用程序产品名称(标题)或描述可以安全更改。

您可以使用选项 nsis.guid 显式设置 guid,但不建议这样做 - 请考虑使用 appId

设置应用程序用户模型 ID (AUMID) 为应用程序的 appId 也很重要,以便 Windows 8/8.1 上的通知能够正常工作,并且 Windows 10 通知默认情况下在通知中显示应用程序图标。AUMID 应在主进程中设置,并在打开任何 BrowserWindow 之前设置,它通常是执行的第一段代码:app.setAppUserModelId(appId)

便携版

要构建便携式应用程序,请将目标设置为 portable(或传递 --win portable)。

对于便携式应用程序,以下环境变量可用

  • PORTABLE_EXECUTABLE_FILE - 便携式可执行文件的路径。
  • PORTABLE_EXECUTABLE_DIR - 便携式可执行文件所在的目录。
  • PORTABLE_EXECUTABLE_APP_FILENAME - 在 文件路径 中使用的经过清理的应用程序名称。

常见问题

如何将默认安装目录更改为自定义目录?

这是一个非常特殊的要求。如果您不确定,请不要这样做。添加 自定义宏

!macro preInit
  SetRegView 64
  WriteRegExpandStr HKLM "${INSTALL_REGISTRY_KEY}" InstallLocation "C:\MyApp"
  WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "C:\MyApp"
  SetRegView 32
  WriteRegExpandStr HKLM "${INSTALL_REGISTRY_KEY}" InstallLocation "C:\MyApp"
  WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "C:\MyApp"
!macroend
是否可以制作一个允许配置用户/机器安装的单个安装程序?

是的,您需要切换到辅助安装程序(而不是默认的单击安装程序)。

package.json

"build": {
  "nsis": {
    "oneClick": false
  }
}
electron-builder.yml
nsis:
  oneClick: false

配置

Electron-Builder / app-builder-lib / NsisOptions

扩展

被扩展

属性

allowElevation?

readonly optional allowElevation: boolean

仅限辅助安装程序。 允许请求提升权限。如果为 false,用户将必须使用提升的权限重新启动安装程序。

默认值
true

allowToChangeInstallationDirectory?

readonly optional allowToChangeInstallationDirectory: boolean

仅限辅助安装程序。 是否允许用户更改安装目录。

默认值
false

artifactName?

readonly optional artifactName: null | string

工件文件名模板。默认为 ${productName} Setup ${version}.${ext}

覆盖

TargetSpecificOptions.artifactName


buildUniversalInstaller?

readonly optional buildUniversalInstaller: boolean

禁用构建目标配置中指定的架构的通用安装程序 Web 安装程序不支持

默认值
true

createDesktopShortcut?

readonly optional createDesktopShortcut: boolean | "always"

是否创建桌面快捷方式。如果设置为 always,则即使被用户删除,也会在重新安装时重新创建。

默认值
true
继承自

CommonWindowsInstallerConfiguration.createDesktopShortcut


createStartMenuShortcut?

readonly optional createStartMenuShortcut: boolean

是否创建开始菜单快捷方式。

默认值
true
继承自

CommonWindowsInstallerConfiguration.createStartMenuShortcut


customNsisBinary?

readonly optional customNsisBinary: null | CustomNsisBinary

允许您提供自己的 makensis,例如支持通过 LogSet 和 LogText 进行调试日志记录的版本。(日志记录还需要选项 debugLogging = true

继承自

CommonNsisOptions.customNsisBinary


deleteAppDataOnUninstall?

readonly optional deleteAppDataOnUninstall: boolean

仅限一键安装程序。 是否在卸载时删除应用程序数据。

默认值
false

displayLanguageSelector?

readonly optional displayLanguageSelector: boolean

是否显示语言选择对话框。不推荐使用(默认情况下将使用操作系统语言检测)。

默认值
false

guid?

readonly optional guid: null | string

请参阅 GUID vs 应用程序名称

继承自

CommonNsisOptions.guid


include?

readonly optional include: null | string

NSIS 包含脚本的路径,用于自定义安装程序。默认为 build/installer.nsh。请参阅 自定义 NSIS 脚本


installerHeader?

readonly optional installerHeader: null | string

仅限辅助安装程序。 MUI_HEADERIMAGE,相对于构建资源或项目目录。

默认值
build/installerHeader.bmp

installerHeaderIcon?

readonly optional installerHeaderIcon: null | string

仅限一键安装程序。 标题图标(进度条上方)的路径,相对于构建资源或项目目录。默认为 build/installerHeaderIcon.ico 或应用程序图标。


installerIcon?

readonly optional installerIcon: null | string

安装程序图标的路径,相对于构建资源或项目目录。默认为 build/installerIcon.ico 或应用程序图标。


installerLanguages?

readonly optional installerLanguages: null | string | string[]

安装程序语言(例如 en_USde_DE)。仅在您了解自己在做什么以及为了什么目的时才更改。


installerSidebar?

readonly optional installerSidebar: null | string

仅限辅助安装程序。 MUI_WELCOMEFINISHPAGE_BITMAP,相对于构建资源或项目目录。默认为 build/installerSidebar.bmp${NSISDIR}\\Contrib\\Graphics\\Wizard\\nsis3-metro.bmp。图片尺寸 164 × 314 像素。


language?

readonly optional language: null | string

LCID Dec,默认为 1033英语 - 美国)。


license?

readonly optional license: null | string

EULA 许可文件的路径。默认为 license.txteula.txt(或大写变体)。除了 txt 之外,还支持 rtfhtml(不要忘记为链接使用 target="_blank")。

支持不同语言的多个许可文件 - 使用语言后缀(例如 _de_ru)。例如,在构建资源中创建文件 license_de.txtlicense_en.txt。如果操作系统语言为德语,将显示 license_de.txt。请参阅 语言代码到名称 的映射。

将根据用户操作系统语言选择适当的许可文件。


readonly optional menuCategory: string | boolean

是否为开始菜单快捷方式和程序文件目录创建子菜单。如果为 true,将使用公司名称。或字符串值。

默认值
false
继承自

CommonWindowsInstallerConfiguration.menuCategory


multiLanguageInstaller?

readonly optional multiLanguageInstaller: boolean

是否创建多语言安装程序。默认为 unicode 选项值。


oneClick?

readonly optional oneClick: boolean

是否创建一键安装程序或辅助安装程序。

默认值
true
覆盖

CommonWindowsInstallerConfiguration.oneClick


packElevateHelper?

readonly optional packElevateHelper: boolean

是否打包 elevate 可执行文件(如果使用每机器安装程序或将来可能使用,则 electron-updater 需要)。如果 perMachine 设置为 true,则忽略。

默认值
true

perMachine?

readonly optional perMachine: boolean

是否为辅助安装程序显示安装模式安装程序页面(选择每机器或每用户)。或者安装是否始终针对所有用户(每机器)。

如果 oneClicktrue(默认值):是否为所有用户安装(每机器)。

如果 oneClickfalseperMachinetrue:没有安装模式安装程序页面,始终每机器安装。

如果 oneClickfalseperMachinefalse(默认值):安装模式安装程序页面。

默认值
false
覆盖

CommonWindowsInstallerConfiguration.perMachine


preCompressedFileExtensions?

readonly optional preCompressedFileExtensions: null | string | string[]

不会压缩的文件的文件扩展名。仅适用于 extraResourcesextraFiles 文件。

默认值
[".avi", ".mov", ".m4v", ".mp4", ".m4p", ".qt", ".mkv", ".webm", ".vmdk"]

publish?

optional publish: Publish

继承自

TargetSpecificOptions.publish


removeDefaultUninstallWelcomePage?

readonly optional removeDefaultUninstallWelcomePage: boolean

仅限辅助安装程序。 删除默认卸载欢迎页面。

默认值
false

runAfterFinish?

readonly optional runAfterFinish: boolean

是否在完成后运行已安装的应用程序。对于辅助安装程序,将删除相应的复选框。

默认值
true
继承自

CommonWindowsInstallerConfiguration.runAfterFinish


script?

readonly optional script: null | string

NSIS 脚本的路径,用于自定义安装程序。默认为 build/installer.nsi。请参阅 自定义 NSIS 脚本


selectPerMachineByDefault?

readonly optional selectPerMachineByDefault: boolean

是否在安装模式安装程序页面上将每机器或每用户安装设置为默认选择。

默认值
false

shortcutName?

readonly optional shortcutName: null | string

将用于所有快捷方式的名称。默认为应用程序名称。

继承自

CommonWindowsInstallerConfiguration.shortcutName


unicode?

readonly optional unicode: boolean

是否创建 Unicode 安装程序

默认值
true
继承自

CommonNsisOptions.unicode


uninstallDisplayName?

readonly optional uninstallDisplayName: string

控制面板中的卸载程序显示名称。

默认值
${productName} ${version}

uninstallerIcon?

readonly optional uninstallerIcon: null | string

卸载程序图标的路径,相对于构建资源或项目目录。默认为 build/uninstallerIcon.ico 或应用程序图标。


uninstallerSidebar?

readonly optional uninstallerSidebar: null | string

仅限辅助安装程序。 MUI_UNWELCOMEFINISHPAGE_BITMAP,相对于构建资源或项目目录。默认为 installerSidebar 选项或 build/uninstallerSidebar.bmpbuild/installerSidebar.bmp${NSISDIR}\\Contrib\\Graphics\\Wizard\\nsis3-metro.bmp


warningsAsErrors?

readonly optional warningsAsErrors: boolean

如果 warningsAsErrorstrue(默认值):NSIS 会将警告视为错误。如果 warningsAsErrorsfalse:NSIS 将允许警告。

默认值
true
继承自

CommonNsisOptions.warningsAsErrors