如何将 Electron 应用程序部署为 Windows 中的可执行文件或可安装文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31286924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:10:35  来源:igfitidea点击:

How to deploy an Electron app as an executable or installable in Windows?

windowsdeploymentdesktop-applicationelectron

提问by Amirado

I want to generate a unique .exefile to execute the app or a .msito install the application. How to do that?

我想生成一个唯一的.exe文件来执行应用程序或.msi安装应用程序。怎么做?

采纳答案by Alex Warren

You can package your program using electron-packagerand then build a single setup EXE file using InnoSetup.

您可以使用electron-packager打包您的程序,然后使用InnoSetup构建单个安装 EXE 文件。

回答by Tharif

Since most answers dont have step by step instructions on packaging, let me post how i got to package the electron app.

由于大多数答案没有关于包装的分步说明,让我发布我如何打包电子应用程序。

We will be installing electron-packager first.

我们将首先安装电子包装器。

Electron Packageris a command line tool and Node.js library that bundles Electron-based application source code with a renamed Electron executable and supporting files into folders ready for distribution.

Electron Packager是一个命令行工具和 Node.js 库,它将基于 Electron 的应用程序源代码与重命名的 Electron 可执行文件和支持文件捆绑到准备分发的文件夹中。

Install electron-packager : run following command in windows cmd

安装电子打包器: 在 windows cmd 中运行以下命令

npm install -g electron-packager --save-dev

Next, lets package our app for windowsx64:

接下来,让我们为 windowsx64 打包我们的应用程序:

electron-packager appdirectory appName --platform=win32 --arch=x64 --electron-version=1.4.3

回答by Anavar Bharmal

You can also try with the electron-boilerplate. Which has 'release' task of gulp and it will create single ready to go executable file for all cross platform. You only need to build application from all three platform to generate particular platform executable.So you don't need to install any third party tool.

您也可以尝试使用电子样板。它具有 gulp 的“发布”任务,它将为所有跨平台创建单个准备就绪的可执行文件。您只需要从所有三个平台构建应用程序即可生成特定平台的可执行文件。因此您无需安装任何第三方工具。

回答by JerryGoyal

There are so many good modules which generate single installer *exefile. Check out any of these:

有很多好的模块可以生成单个安装程序*exe文件。查看以下任何一项:

  • electron-builder(genrates executable for Windows,Mac and Linux, have server-less app auto-update feature,code signing, publishing etc, less boilerplate)

  • electron-forge(genrates executable for Windows,Mac and Linux, it not just package apps but helps you create them as well, more boilerplate)

  • windows-installer(easy to use, light weight, and generates only exe file)

  • 电子生成器(为 Windows、Mac 和 Linux 生成可执行文件,具有无服务器应用程序自动更新功能、代码签名、发布等,更少样板)

  • 电子伪造(为 Windows、Mac 和 Linux 生成可执行文件,它不仅打包应用程序,还帮助您创建它们,更多样板)

  • windows-installer(使用方便,重量轻,只生成exe文件)

(still confused which one to pick? compare here)

(仍然困惑选择哪一个?在这里比较)

回答by Panos Bakas

2020 UpdateYou can use electron-builderto create portable .exe file for your electron app. All you need to do is install electron-builder with yarn add electron-builder --devThen create a package.jsonfile like this(this is just for portable .exe):

2020 更新您可以使用electron-builder为您的电子应用程序创建可移植的 .exe 文件。您需要做的就是安装电子生成器,yarn add electron-builder --dev然后创建一个这样的package.json文件(这仅适用于便携式 .exe):

{
  "name": "my-electron-app",
  "productName": "electron app",
  "version": "1.0.0",
  "description": "an electron app",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "dist": "electron-builder"
  },
  "devDependencies": {
    "electron": "^8.0.2",
    "electron-builder": "^22.3.2"
  },
  "build": {
    "appId": "com.electron.app",
    "win": {
      "target": "portable"
    },
    "portable": {
      "unicode": false,
      "artifactName": "my_electron_app.exe"
    }
  }
}

回答by tpikachu

To package the electron app as installable or executable. electron-buildershould be the best choice. And it's easy to configure and we can use electron auto-updater too. Here is the example of electron-builder.json

将电子应用程序打包为可安装或可执行的。electron-builder应该是最好的选择。而且它很容易配置,我们也可以使用电子自动更新程序。这里是一个例子electron-builder.json

{
  "publish": {
    // This can be also 's3', 'github'... based on which server are you using for publish
    // https://www.electron.build/configuration/publish
    "provider": "generic",

    // Feed URL but github provider case, other fields will be required. 'repo', 'owner'...
    "url": "https://myappserver.com/updates/"
  },

  "productName": "My App",
  "appId": "com.myapp.app",

  "directories": {
    // The icon and background in 'buildResources' will be used as app Icon and dmg Background
    "buildResources": "buildResources",

    // output is directory where the packaged app will be placed
    "output": "release"
  },

  // The files which will be packed
  "files": ["src/", "node_modules/", "package.json"],

  "mac": {
    "target": ["dmg", "zip"], // Also can be, 'pkg', ...
    "artifactName": "${productName}-${version}-${os}.${ext}"
  },
  "win": {
    "target": ["nsis", "zip"], // Also can be, 'portable', ...
    "artifactName": "${productName}-${version}-${os}.${ext}"
  },
  "linux": {
    "target": ["AppImage"],
    "artifactName": "${productName}-${version}-${os}.${ext}"
  },
  "dmg": {
    "title": "${productName}-${version}",
    "contents": [
      {
        "x": 300,
        "y": 360
      },
      {
        "x": 490,
        "y": 360,
        "type": "link",
        "path": "/Applications"
      }
    ]
  }
}

Of course, we can add other configurations such as nsis, extraFiles, afterPack, afterSign...

当然,我们可以添加其他配置,例如nsis, extraFiles, afterPack, afterSign...

The above are well-used. You can check details and other fields at here https://www.electron.build/configuration/publish

以上都是很好用的。您可以在此处查看详细信息和其他字段https://www.electron.build/configuration/publish

We can define this configuration at the inside of package.jsonor as an isolated file but the name should be electron-builder.jsonor electron-builder.ymlat the project root directory.

我们可以在内部package.json或作为隔离文件定义此配置,但名称应为electron-builder.jsonelectron-builder.yml在项目根目录中。

And in addition, for auto-update. We should upload the installers(dmg, exe, appImage) among with zip, blockmapand latest-{OS_Name}.ymlfiles.

此外,用于自动更新。我们应该将安装程序(dmg、exe、appImage)上传到zipblockmaplatest-{OS_Name}.yml文件中。

回答by Wilmar Barb

well ... this will work but the idea is to run the .exe without need of install it in the pc ... another solution is use Autoplay media Studio for wrap you package generated by electron and make one executable or other solution is to use thinstall vmware... The cons both are commercial so you have to paid for them...

好吧......这会起作用,但想法是运行.exe而无需将其安装在PC中......另一种解决方案是使用Autoplay media Studio来包装由电子生成的包并制作一个可执行文件或其他解决方案是使用thinstall vmware...缺点都是商业性的,所以你必须为它们付费...

回答by nivs1978

I first tried the electron-packager but it produced a lot of .dll files and still could not run.

我首先尝试了电子打包器,但它生成了很多 .dll 文件,但仍然无法运行。

What did work for me was:

对我有用的是:

npm install
npm run dist --ia32

This produced a single self contained exe, no other files needed to run the application.

这产生了一个独立的 exe,运行应用程序不需要其他文件。

回答by selanac82

You might be able to "wrap" the entire electron app in a .NET project. Then the single executable that it creates can then just "internally" run the electron app.

您也许可以将整个电子应用程序“包装”在 .NET 项目中。然后它创建的单个可执行文件就可以“在内部”运行电子应用程序。