Electron Desktop App Development · 课时

打包您的 Electron 应用

了解如何使用 `electron-builder` 等工具,为不同操作系统(Windows、macOS、Linux)打包 Electron 应用以便分发

第 3 / 4 课12 个步骤

打包您的 Electron 应用 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Package Your App?

When you develop an Electron app, it runs using Node.js and Chromium. However, your users won't have Node.js installed or know how to run your development files.

Packaging your application means bundling all its necessary files—your code, Electron runtime, and any assets—into a single, easy-to-distribute executable file or installer, like a .exe for Windows or a .dmg for macOS.

Meet Electron Builder

electron-builder is the most popular and robust solution for packaging and distributing Electron applications. It simplifies the complex process of preparing your app for various operating systems.

  • Cross-Platform: Build for Windows, macOS, and Linux from a single codebase.
  • Installers: Generate platform-specific installers (e.g., NSIS for Windows, DMG for macOS).
  • Auto-Updates: Integrates seamlessly with auto-update mechanisms.
  • Customization: Offers extensive options for icons, product names, and more.

Installing Electron Builder

First, you need to add electron-builder as a development dependency to your project. Open your terminal in your project's root directory and run:

npm install --save-dev electron-builder

Basic Project Setup

Before packaging, ensure your package.json is correctly set up. It defines your application's basic information and entry point. Here's a minimal example:

  • name: Your app's unique identifier.
  • version: Current version.
  • main: Path to your main Electron script.
  • scripts.start: Command to run your app in development.
{
  "name": "my-electron-app",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  }
}

Configure package.json for Build

Now, let's add the build configuration to your package.json. This block tells electron-builder how to package your app.

  • appId: A unique identifier for your app (e.g., com.yourcompany.yourapp).
  • productName: The name users will see for your app.
  • directories.output: Where the packaged files will be saved (commonly dist).
  • scripts.build: The command to trigger the build.
{
  "name": "my-electron-app",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "build": "electron-builder"
  },
  "build": {
    "appId": "com.yourcompany.yourapp",
    "productName": "My Awesome App",
    "directories": {
      "output": "dist"
    }
  }
}

Building for Windows

For Windows, electron-builder can create various targets. The most common are nsis (Nullsoft Scriptable Install System) for a standard installer, or portable for a single executable that doesn't require installation.

You can specify the target when running the build command:

npm run build -- --win nsis

Building for macOS

On macOS, typical distribution formats include dmg (Disk Image) for a guided installation, or zip for a direct archive of the .app bundle.

It's also good practice to specify a category for macOS apps.

npm run build -- --mac dmg

Building for Linux

Linux offers several packaging formats to cater to different distributions:

  • deb: For Debian-based systems (like Ubuntu).
  • rpm: For Red Hat-based systems (like Fedora).
  • AppImage: A universal format that runs on most Linux distributions.

Here's how to build an AppImage:

npm run build -- --linux AppImage

Customizing with Icons

A custom application icon is essential for brand recognition. You can specify a single icon path in your package.json's build section. electron-builder will automatically generate different sizes for various platforms.

It's recommended to place your main icon (e.g., a .png) in a build directory in your project root.

{
  "build": {
    "appId": "com.yourcompany.yourapp",
    "productName": "My Awesome App",
    "directories": {
      "output": "dist"
    },
    "icon": "build/icon.png"
  }
}

Run Your Build!

With your package.json configured, executing the build command is straightforward. This command will start the packaging process, which might take a few minutes depending on your project size and internet speed (to download Electron binaries).

Once complete, you'll find your packaged application files in the dist directory.

npm run build

Packaging Features Quiz

Which of these are valid reasons to use electron-builder for packaging your Electron app?

Recap: Ready for Distribution

You've learned the essentials of packaging your Electron application!

  • Packaging bundles your app into a distributable format.
  • electron-builder is the go-to tool for cross-platform packaging.
  • Configuration happens in package.json, defining app metadata and build targets.
  • You can create installers for Windows (nsis), macOS (dmg), and various Linux formats (AppImage, deb, rpm).

Now your app is ready to be shared with the world!

免费开始

用 AI 导师学习 JavaScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
47

常见问题解答

「打包您的 Electron 应用」课时是免费的吗?

是的 — 「打包您的 Electron 应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。

「打包您的 Electron 应用」这节课中我会学到什么?

了解如何使用 `electron-builder` 等工具,为不同操作系统(Windows、macOS、Linux)打包 Electron 应用以便分发 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Electron Desktop App Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「打包您的 Electron 应用」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Electron Desktop App Development 课中编写并运行代码吗?

能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 主进程与渲染进程
  2. 进程间通信(IPC)
  3. 打包您的 Electron 应用
  4. 管理多个窗口
← 返回 Electron Desktop App Development