Electron Desktop App Development · บทเรียน

การจัดแพ็กเกจแอป Electron

ค้นพบวิธีจัดแพ็กเกจแอปพลิเคชัน Electron เพื่อเผยแพร่บนระบบปฏิบัติการต่าง ๆ (Windows, macOS, Linux) โดยใช้เครื่องมืออย่าง `electron-builder`

บทเรียน 3 จาก 412 ขั้นตอน

การจัดแพ็กเกจแอป Electron เป็นบทเรียน Electron Desktop App Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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!

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
47

คำถามที่พบบ่อย

บทเรียน “การจัดแพ็กเกจแอป Electron” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดแพ็กเกจแอป Electron” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Electron Desktop App Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดแพ็กเกจแอป Electron”

ค้นพบวิธีจัดแพ็กเกจแอปพลิเคชัน Electron เพื่อเผยแพร่บนระบบปฏิบัติการต่าง ๆ (Windows, macOS, Linux) โดยใช้เครื่องมืออย่าง `electron-builder` คุณปฏิบัติ Electron Desktop App Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Electron Desktop App Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Electron Desktop App Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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