为什么选择 Electron
🌐 Why Electron
Electron 是一个框架,使开发者能够通过结合 Web 技术(HTML、JavaScript、CSS)与 Node.js 以及本地代码来构建适用于 macOS、Windows 和 Linux 的跨平台桌面应用。它是开源的,采用 MIT 许可证,并且可免费用于商业和个人用途。在本文档中,我们将解释为什么公司和开发者选择 Electron。
🌐 Electron is a framework enabling developers to build cross-platform desktop applications for macOS, Windows, and Linux by combining web technologies (HTML, JavaScript, CSS) with Node.js and native code. It is open-source, MIT-licensed, and free for both commercial and personal use. In this document, we’ll explain why companies and developers choose Electron.
我们可以通过两个问题来分解 Electron 的好处:首先,为什么你应该使用网页技术来构建你的应用?然后,为什么你应该选择 Electron 作为实现这一目标的框架?
🌐 We can split up the benefits of Electron in two questions: First, why should you use web technologies to build your application? Then, why should you choose Electron as the framework to do so?
如果你已经在你的应用中使用了网页技术,你可以直接跳到下面的 Why Electron? 部分。
🌐 If you’re already using web technologies for your application, you can skip straight to the Why Electron? section below.
为什么选择 Web 技术
🌐 Why choose web technologies
Web 技术包括 HTML、CSS、JavaScript 和 WebAssembly。它们是现代互联网的门面。这些技术已经成为构建用户界面的最佳选择——无论是面向消费者的应用还是关键业务应用。这对于需要在浏览器中运行的应用以及无法通过浏览器访问的桌面应用都是如此。我们在这里大胆主张,这不仅适用于需要在多个操作系统上运行的跨平台应用,而且总体上都是正确的。
🌐 Web technologies include HTML, CSS, JavaScript, and WebAssembly. They’re the storefront of the modern Internet. Those technologies have emerged as the best choice for building user interfaces — both for consumer applications as well as mission-critical business applications. This is true both for applications that need to run in a browser as well as desktop applications that are not accessible from a browser. Our bold claim here is that this isn’t just true for cross-platform applications that need to run on multiple operating systems but true overall.
例如,NASA 实际的任务控制就是用网页技术开发的。彭博终端,每个金融机构都有的计算机系统,也是用网页技术开发的,并运行在 Chromium 中。每位用户每年费用为 25,000 美元。麦当劳的点餐自助机,为全球最大的食品零售商提供服务,也是完全基于 Chromium 构建的。SpaceX 的龙飞船 2 号舱使用 Chromium 来显示其界面。意思很清楚:网页技术是构建用户界面的绝佳技术栈。
🌐 As an example, NASA’s actual Mission Control is written with web technologies. The Bloomberg Terminal, the computer system found at every financial institution, is written with web technologies and runs inside Chromium. It costs $25,000 per user, per year. The McDonald’s ordering kiosk, powering the world’s biggest food retailer, is entirely built with Chromium. The SpaceX’s Dragon 2 space capsule uses Chromium to display its interface. You get the point: web technologies are a great tech stack to build user interfaces.
以下是我们(Electron 维护者)押注 Web 的原因。
🌐 Here are the reasons we, the Electron maintainers, are betting on the web.
多功能性
🌐 Versatility
现代版本的 HTML 和 CSS 使你的开发者和设计师能够充分表达自己。网络的展示范例包括 Google Earth、Netflix、Spotify、Gmail、Facebook、Airbnb 或 GitHub。无论你的应用需要哪种界面,你都可以使用 HTML、CSS 和 JavaScript 来实现它。
🌐 Modern versions of HTML and CSS enable your developers and designers to fully express themselves. The web’s showcase includes Google Earth, Netflix, Spotify, Gmail, Facebook, Airbnb, or GitHub. Whatever interface your application needs, you will be able to express it with HTML, CSS, and JavaScript.
如果你希望专注于构建一款出色的产品,而无需弄清楚如何在特定的 UI 框架中实现设计师的愿景,那么 Web 是一个安全的选择。
🌐 If you want to focus on building a great product without figuring out how you can realize your designer’s vision in a specific UI framework, the web is a safe bet.
可靠性
🌐 Reliability
Web 技术是全球用户界面中使用最广泛的基础。它们因此得到了强化。现代计算机从 CPU 到操作系统都进行了优化,以便能够高效运行 Web 技术。用户设备的制造商——无论是 Android 手机还是最新的 MacBook——都会确保设备能够访问网站、在 YouTube 上播放视频或显示电子邮件。反过来,他们也会确保你的应用有一个稳定的基础,即使你只有一个用户。
🌐 Web technologies are the most-used foundation for user interfaces on the planet. They have been hardened accordingly. Modern computers have been optimized from the CPU to the operating system to be good at running web technologies. The manufacturers of your user’s devices—be that an Android phone or the latest MacBook—will ensure that they can visit websites, play videos on YouTube, or display emails. In turn, they’ll also ensure that your app has a stable foundation, even if you have just one user.
如果你希望专注于构建一款出色的产品,而无需调试以前没有人发现的怪异现象,那么 Web 是一个安全的选择。
🌐 If you want to focus on building a great product without debugging a weird quirk that nobody has found before, the web is a safe bet.
互操作性
🌐 Interoperability
无论你需要与哪种提供商或客户数据进行交互,他们可能已经考虑过与网络的集成路径。根据你的技术选择,嵌入一个 YouTube 视频可能只需要 30 秒,或者需要你雇佣一个专门负责流媒体和硬件加速视频解码的团队。在 YouTube 的情况下,使用除提供的播放器之外的任何方式实际上都违反了他们的条款,因此在你实现自己的视频流解码器之前,你很可能会先嵌入一个浏览器框架。
🌐 Whatever provider or customer data you need to interact with, they will have probably thought of an integration path with the web. Depending on your technology choice, embedding a YouTube video either takes 30 seconds or requires you to hire a team devoted to streaming and hardware-accelerated video decoding. In the case of YouTube, using anything other than the provided players is actually against their terms and conditions, so you’ll likely embed a browser frame before you implement your own video streaming decoder.
如果你使用网页技术开发应用,几乎没有平台是无法运行的。几乎所有带有显示屏的设备——无论是自动取款机、汽车信息娱乐系统、智能电视、冰箱,还是任天堂Switch——都能显示网页技术。如果你想实现跨平台,网页是一个可靠的选择。
🌐 There will be virtually no platform where your app cannot run if you build it with web technologies. Virtually all devices with a display—be that an ATM, a car infotainment system, a smart TV, a fridge, or a Nintendo Switch—come with means to display web technologies. The web is a safe bet if you want to be cross-platform.
普遍性
🌐 Ubiquity
找到有使用网络技术开发经验的开发者很容易。如果你是开发者,你可以很容易地在 Google、Stack Overflow、GitHub 或你选择的编程 AI 上找到问题的答案。无论你需要解决什么问题,很可能之前有人已经很好地解决过——而且你可以在网上找到这个问题的答案。
🌐 It’s easy to find developers with experience building with web technologies. If you’re a developer, it’ll be easy to find answers to your questions on Google, Stack Overflow, GitHub, or a coding AI of your choice. Whatever problem you need to solve, it’s likely that somebody has solved it well before—and that you can find the answer to the puzzle online.
如果你希望专注于构建一款出色的产品,并拥有充足的资源和材料,那么 Web 是一个安全的选择。
🌐 If you want to focus on building a great product with ample access to resources and materials, the web is a safe bet.
为什么选择 Electron
🌐 Why choose Electron
Electron 将 Chromium、Node.js 以及编写自定义本地代码的能力结合到一个框架中,用于构建强大的桌面应用。使用 Electron 的主要原因有三个:
🌐 Electron combines Chromium, Node.js, and the ability to write custom native code into one framework for building powerful desktop applications. There are three main reasons to use Electron:
企业级
🌐 Enterprise-grade
Electron 可靠、安全、稳定且成熟。它是企业打造旗舰产品的首选。我们在主页上列出了一些使用它的公司名单,但仅在聊天应用中,Slack、Discord 和 Signal 都是用 Electron 构建的。在 AI 应用方面,OpenAI 的 ChatGPT 和 Anthropic 的 Claude 都使用了 Electron。Visual Studio Code、Loom、Canva、Notion、Docker 以及无数其他领先的软件开发者都选择了 Electron。
🌐 Electron is reliable, secure, stable, and mature. It is the premier choice for companies building their flagship product. We have a list of some of those companies on our homepage, but just among chat apps, Slack, Discord, and Signal are built with Electron. Among AI applications, both OpenAI’s ChatGPT and Anthropic’s Claude use Electron. Visual Studio Code, Loom, Canva, Notion, Docker, and countless other leading developers of software bet on Electron.
我们确实优先考虑让 Electron 易于使用,并让开发者感到愉快。这很可能是 Electron 今天如此受欢迎的主要原因——但真正让 Electron 能够持续发展和繁荣的是维护者专注于让 Electron 尽可能稳定、安全、高性能,并能够应对终端用户的关键任务使用场景。我们正在构建的 Electron,旨在能够在无法修复的漏洞、未打补丁的安全漏洞和任何形式的停机都是最坏情况的场景中使用。
🌐 We did make it a priority to make Electron easy to work with and a delight for developers. That’s likely the main reason why Electron became as popular as it is today — but what keeps Electron alive and thriving is the maintainer’s focus on making Electron as stable, secure, performant, and capable of mission-critical use cases for end users as possible. We’re building an Electron that is ready to be used in scenarios where unfixable bugs, unpatched security holes, and outages of any kind are worst-case scenarios.
成熟
🌐 Mature
我们目前的估计是,地球上大多数台式计算机至少运行一个 Electron 应用。Electron 的发展重视维护者团队的人才,培养在持续维护管理中卓越且可持续的工程实践,并主动邀请投注于 Electron 的公司直接为项目做出贡献。我们是 OpenJS 基金会的一个影响力项目,而 OpenJS 基金会本身是 Linux 基金会的一部分。我们与其他基金会项目共享资源和专长,比如 Node.js、ESLint、Webpack,以及 Linux 内核或 Kubernetes。
🌐 Our current estimation is that most desktop computers on the planet run at least one Electron app. Electron has grown by prioritizing talent in its maintainer group, fostering excellent and sustainable engineering practices in managing the ongoing maintenance, and proactively inviting companies betting on Electron to directly contribute to the project. We’re an impact project with the OpenJS foundation, which is itself a part of the Linux foundation. We share resources and expertise with other foundation projects like Node.js, ESLint, Webpack - or the Linux Kernel or Kubernetes.
在实践中,所有这些对你(开发者)意味着什么?
🌐 What does all of that mean for you, a developer, in practice?
- 可靠的发布计划:Electron 将与每个第二个主要 Chromium 版本同步发布新主版本,通常与 Chromium 发布的日期相同。为了实现这一目标,需要投入大量工作,包括构建进程和工具的工作,以及每周投入的实际小时数。
- 没有独裁者:有时候,押注一项技术也意味着你必须押注某个人或某家公司。相应地,这要求你信任该个人或公司永远不会崩溃、直接与你对抗,或做出任何其他极端举动,从而迫使你重新考虑整个技术栈。Electron 由多家公司维护(微软、Slack/Salesforce、Notion 等),并将继续欢迎更多希望确保自己“在决策桌上占有一席之地”的公司加入。
稳定性、安全性、性能
🌐 Stability, security, performance
Electron 通过将最新版本的 Chromium、V8 和 Node.js 直接打包到应用二进制文件中,为所有目标平台(macOS、Windows、Linux)提供最佳体验。在以最高稳定性、安全性和性能运行和呈现网页内容时,我们目前认为这个技术栈是“同类最佳”的。
🌐 Electron delivers the best experience on all target platforms (macOS, Windows, Linux) by bundling the latest version of Chromium, V8, and Node.js directly with the application binary. When it comes to running and rendering web content with utmost stability, security, and performance, we currently believe that stack to be “best in class”.
为什么要打包任何东西
🌐 Why bundle anything at all
你可能会想,为什么当大多数现代操作系统已经自带浏览器和某种形式的网页视图时,我们还要将 Chromium 的网页堆栈与我们的应用打包在一起。打包不仅会大幅增加 Electron 维护者的工作量,还会增加 Electron 应用的总磁盘大小(大多数应用都超过 100MB)。许多 Electron 维护者曾经开发过确实使用嵌入式网页视图的应用——并且自那以后,他们已经接受了磁盘大小增加和维护者工作量增加的这一值得付出的权衡。
🌐 You might wonder why we bundle Chromium’s web stack with our apps when most modern operating systems already ship a browser and some form of web view. Bundling doesn’t just increase the amount of work for Electron maintainers dramatically, it also increases the total disk size of Electron apps (most apps are >100MB). Many Electron maintainers once developed applications that did make use of embedded web views — and have since accepted the increased disk size and maintainer work as a worthy trade-off.
当使用操作系统自带的网页视图时,你会受到需要支持的最旧操作系统版本中所包含的浏览器版本的限制。我们发现这种方法存在以下问题:
🌐 When using an operating system's built-in web view, you're limited by the browser version included in the oldest operating system version you need to support. We have found the following problems with this approach:
- 稳定性:现代的网页技术栈非常复杂,因此你迟早会遇到漏洞。如果你使用操作系统自带的网页视图,唯一的解决办法就是让你的客户升级他们的操作系统。如果那台机器无法升级(因为无法升级到最新的 macOS 或 Windows 11),你就得让他们购买一台新电脑。如果你运气不好,你可能会因此失去一个重要客户,因为他们不会为了让一个团队尝试你创业公司的应用而升级整个拥有数千台机器的设备队伍。在这种情况下,你 没有任何补救办法。即使发生这种风险,对雇佣 Electron 维护者的公司来说也是无法接受的。
- 安全性: 类似于你可以通过发布应用更新来修复稳定性漏洞,你也可以向你的应用发布安全修复,而无需要求用户升级他们的操作系统。即使操作系统提供商优先考虑更新其内置浏览器,我们也没有看到他们以同样的紧迫性可靠地更新内置 Web 视图。打包一个 Web 渲染器可以让你,开发者,掌握控制权。
- 性能: 对于简单的 HTML 文档,内置的 web 视图有时会比包含框架的应用使用更少的资源。对于较大的应用,根据我们的经验,我们可以通过最新版本的 Chromium 提供比内置 web 视图更好的性能。你可能会认为内置视图可以与其他应用和操作系统共享大量资源——但出于安全原因,应用必须运行在各自的沙箱中,彼此隔离。在这种情况下,问题在于操作系统的 web 视图是否比 Chromium 更高效。根据我们在许多应用中的经验,将 Chromium 和 Node.js 打包在一起使我们能够构建更好、更高性能的体验。
为什么打包 Chromium 和 Node.js
🌐 Why bundle Chromium and Node.js
Electron 的目标是让其支持的应用提供尽可能最佳的用户体验,其次是尽可能最佳的开发者体验。Chromium 目前是最好的跨平台渲染架构。Node.js 使用 Chromium 的 JavaScript 引擎 V8,使我们能够结合两者的优势。
🌐 Electron aims to enable the apps it supports to deliver the best possible user experience, followed by the best possible developer experience. Chromium is currently the best cross-platform rendering stack available. Node.js uses Chromium’s JavaScript engine V8, allowing us to combine the powers of both.
- 需要时使用原生代码:得益于 Node.js 成熟的原生插件系统,你始终可以编写原生代码。没有任何系统 API 是你无法触及的。无论是 macOS、Windows 还是 Linux 的功能,只要你能用 C、C++、Objective-C、Rust 或其他原生语言实现,你就能在 Electron 中实现。同样地,这为你——开发者——提供了最大的控制权。使用 Electron,你可以使用网页技术,同时不必仅限于网页技术。
开发者体验
🌐 Developer experience
总而言之,我们的目标是打造一个成熟的、企业级的、可用于关键任务应用的 Electron。我们优先考虑可靠性、稳定性、安全性和性能。也就是说,你也可能因为其开发者体验而选择 Electron:
🌐 To summarize, we aim to build an Electron that is mature, enterprise-grade, and ready for mission-critical applications. We prioritize reliability, stability, security, and performance. That said, you might also choose Electron for its developer experience:
- 强大的生态系统:你在 npm 上找到的任何东西都可以在 Electron 中运行。任何关于如何使用 Node.js 的资源同样适用于 Electron。此外,Electron 本身也有一个蓬勃发展的生态系统——包括大量安装程序、更新程序、更深的操作系统集成等选择。
- 丰富的内置功能: 在过去的十年里,Electron 的核心增加了许多你在构建应用时可能需要的原生功能。Electron 使用 C++ 和 Objective-C 编写,提供了数十个易于使用的 API 用于更深层次的操作系统集成——例如用于透明或奇形控件的高级窗口自定义、接收来自 Apple 推送通知网络的推送通知,或处理应用的自定义 URL 协议。
- 开源:整个堆栈都是开源的,你可以自由检查。这确保了你在未来可以自由添加任何功能或修复你可能遇到的任何错误。
- 需要时使用原生代码: 值得一再强调的是,Electron 允许你混合使用网页技术与 C++、C、Objective-C、Rust 以及其他原生语言。无论是 SQLite、完整的 LLM,还是仅仅调用某个特定的原生 API,Electron 都能轻松实现。
为什么选择其他东西
🌐 Why choose something else
如上所述,网络是构建界面的一个极好的平台。但这并不意味着我们这些维护者会用 HTML 和 CSS 构建所有东西。以下是一些值得注意的例外情况:
🌐 As outlined above, the web is an amazing platform for building interfaces. That doesn’t mean that we, the maintainers, would build everything with HTML and CSS. Here are some notable exceptions:
资源受限环境与物联网: 在内存或处理能力非常有限的场景下(例如在低功耗 ARM Cortex-M 上只有一兆字节内存和 100MHz 处理能力),你可能需要使用低级语言直接与显示器进行通信,以输出基本的文本和图片。即使在性能稍高的单芯片设备上,你也可能需要考虑使用嵌入式 UI 框架。一个经典的例子是智能监视。
小型磁盘占用:压缩后的 Electron 应用通常约为 80 到 100 兆字节。如果对较小的磁盘占用有严格要求,你将不得不使用其他方案。
操作系统用户界面框架和库:通过允许你编写原生代码,Electron 可以完成任何原生应用可以做的事情,包括使用操作系统的 UI 组件,如 WinUI、SwiftUI 或 AppKit。实际上,大多数 Electron 应用很少使用这种能力。如果你希望你的应用大部分使用操作系统提供的界面组件,最好为你想要支持的每个操作系统构建完全原生的应用。这并不是说在 Electron 上不可能实现,只是整体开发进程可能会更轻松。
游戏与实时图形: 如果你正在开发需要复杂实时3D图形的高性能游戏或应用,本地框架如 Unity、Unreal Engine 或 DirectX/OpenGL 将提供更好的性能和更直接的图形硬件访问。网络爱好者可能会指出一些注意事项,例如即便 Unreal Engine 也打包了 Chromium——或者 WebGPU 和 WebGL 正在快速发展,许多游戏引擎,包括这里列出的那些,现在也可以将游戏导出为可以在浏览器中运行的格式。尽管如此,如果你让我们开发下一款 AAA 游戏,我们很可能会选择除了网页技术之外的其他东西。
嵌入轻量级网站:Electron 应用通常主要是网页应用,只有在有用的地方才会添加少量原生代码。处理密集型的 Electron 应用通常会用 HTML/CSS 编写用户界面,并用 Rust、C++ 或其他原生语言构建后端。如果你打算构建一个主要是原生的应用,同时又希望在特定视图中显示一个小型网站,那么使用操作系统提供的网页视图或类似 ultralight 的方案可能会更合适。