跳转到主内容

为什么选择 Electron

Electron 是一个框架,使开发者能够将 Web 技术(HTML、JavaScript、CSS)、Node.js 及原生代码相结合,构建适用于 macOS、Windows 和 Linux 的跨平台桌面应用程序。 它基于MIT开源许可证,对商业和个人用途均免费。 在本文档中,我们将解释为何 Electron 备受公司和开发者青睐。

我们可以从两个方面来看待 Electron 的优势:首先,为什么要用 Web 技术开发应用? 其次,为什么偏偏要选 Electron?

如果你已经在使用 Web 技术构建应用程序,可以直接跳到下面的 为什么选择 Electron? 部分。

为什么选择 Web 技术

Web 技术包括 HTML、CSS、JavaScript 和 WebAssembly。 它们是现代互联网的基石。 这些技术已成为构建用户界面的最佳选择——无论是对于消费者应用程序还是关键业务型应用程序。 这不仅适用于需要在浏览器中运行的应用程序,也适用于那些无法通过浏览器访问的桌面应用程序。 我们在此大胆断言,这不仅适用于需要在多个操作系统上运行的跨平台应用程序,而且适用于所有类型的应用程序。

例如,美国国家航空航天局 (NASA) 的任务控制中心就是使用 Web 技术编写的。 彭博终端是每个金融机构都能找到的计算机系统,它也是使用 Web 技术编写的,并在 Chromium 中运行。 它的价格是每用户每年 25,000 美元。 为全球大型食品零售商提供支持的麦当劳自助点餐机完全是使用 Chromium 构建的。 SpaceX 的 Dragon 2 太空舱使用 Chromium 来显示其界面。 你可能已经感受到:Web 技术是构建用户界面的绝佳技术栈。

以下是 Electron 维护者们坚定支持 Web 的原因。

多功能性

现代版本的 HTML 和 CSS 能够让开发者和设计师充分表达自己。 Web 技术的应用案例包括 Google Earth、Netflix、Spotify、Gmail、Facebook、Airbnb 或 GitHub。 无论你的应用程序需要什么样的界面,你都可以使用 HTML、CSS 和 JavaScript 来实现。

如果你想专注于打造出色的产品,而无需费力研究如何在特定的 UI 框架中实现设计师的愿景,那么 Web 技术是一个稳妥的选择。

可靠性

Web 技术是全球使用最为广泛的用户界面基础。 它们也因此得到了充分的强化。 从 CPU 到操作系统,现代计算机都经过了优化,可以很好地运行 Web 技术。 用户设备的制造商——无论是 Android 手机还是新款 MacBook——都将确保它们可以访问网站、在 YouTube 上播放视频或显示电子邮件。 相应地,他们也将确保你的应用程序有一个稳定的基础,即使你只有一个用户。

如果你想专注于打造出色的产品,而无需调试以前从未有人发现过的奇怪问题,那么 Web 技术是一个稳妥的选择。

互操作性

无论你需要与哪个服务提供商或客户数据进行交互,他们都可能已经考虑了与 Web 的集成路径。 根据你的技术选择,嵌入 YouTube 视频可能只需 30 秒,也可能需要你聘请一个专门从事流媒体和硬件加速视频解码的团队。 对于 YouTube,使用其提供的播放器之外的任何内容实际上都违反了他们的服务条款,因此你很可能在实现自己的视频流解码器之前先嵌入一个浏览器框架。

如果你使用 Web 技术构建应用程序,则几乎没有平台无法运行。 几乎所有带有显示屏的设备——无论是 ATM 机、车载信息娱乐系统、智能电视、冰箱还是任天堂 Switch——都具备显示 Web 技术的方法。 如果你想实现跨平台,那么 Web 技术是一个稳妥的选择。

普遍性

很容易找到具有 Web 技术开发经验的开发者。 如果你是一名开发者,你将很容易在 Google、Stack Overflow、GitHub 或你所选择的代码 AI 助手上找到问题的答案。 无论你需要解决什么问题,很可能有人已经很好地解决了它——你可以在网上找到解决方案。

如果你想专注于打造出色的产品,并能充分利用各种资源和材料,那么 Web 技术是一个稳妥的选择。

为什么选择 Electron

Electron 将 Chromium、Node.js 和编写自定义原生代码的能力结合到一个框架中,用于构建强大的桌面应用程序。 使用 Electron 的主要原因有三个:

企业级

Electron 可靠、安全、稳定且成熟。 它是众多公司构建其旗舰级产品的首选框架。 我们在主页上列出了一些使用 Electron 的公司,仅在聊天应用中,Slack、Discord 和 Skype 都是使用 Electron 构建的。 在 AI 应用程序中,OpenAI 的 ChatGPT 和 Anthropic 的 Claude 都使用 Electron。 Visual Studio Code、Loom、Canva、Notion、Docker 以及无数其他领先的软件开发商都选择了 Electron。

我们确实将使 Electron 易于使用并为开发者带来愉悦体验作为一项优先事项。 这可能是 Electron 如此受欢迎的主要原因——但保持 Electron 活力和蓬勃发展的是维护者专注于使 Electron 尽可能稳定、安全、高性能,并能够满足最终用户的关键任务型用例。 我们正在构建的 Electron,适用于那些不允许出现无法修复的错误、未修补的安全漏洞和任何类型服务中断的场景。

成熟

据我们目前的估计,全球大多数台式电脑上都运行着至少一个 Electron 应用。 Electron 的发展得益于其维护者团队中的人才优先化,在管理持续维护方面培养了卓越且可持续的工程实践,并主动邀请选择 Electron 的公司直接为该项目做出贡献。 我们是 OpenJS 基金会的一个影响力项目,该基金会本身也是 Linux 基金会的一部分。 我们与 Node.js、ESLint、Webpack 等其他基金会项目,以及 Linux 内核或 Kubernetes 共享资源和专业知识。

对于你这样的开发者,所有这些在实践中意味着什么?

  • 可靠的发布时间表:Electron 将与 Chromium 的每个次要版本同步发布一个新的主要版本,通常和 Chromium 在同一天发布。 为了实现这一目标,需要在构建流程、开发工具以及确保每周的实际工时上付出巨大努力。
  • 去中心化决策:有时,选择一项技术也需要你选择一个人或一家公司。 相应地,这要求你相信该个人或公司永远不会崩溃、产生直接竞争,或者做出任何其他迫使你重新考虑整个技术栈的剧烈举动。 Electron 由一组不同的公司(Microsoft、Slack/Salesforce、Notion 等)维护,并将继续欢迎更多有兴趣确保其“在决策桌上占有一席之地”的公司。

稳定性、安全性与性能

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 upmost stability, security, and performance, we currently believe that stack to be “best in class”.

Why bundle anything at all

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:

  • Stability: The modern web technology stack is complex, and as a result, you’ll sooner or later encounter bugs. If you use the operating system’s web view, your only recourse will be to ask your customers to upgrade their operating system. If no upgrade is available for that machine (because of no ability to upgrade to the latest macOS or Windows 11), you’ll have to ask them to buy a new computer. If you’re unlucky, you’re now losing a major customer because they will not upgrade their entire fleet of thousands of machines just because one team wanted to try your startup’s app. You have no recourse in this situation. Even the risk of that happening is unacceptable to the companies that employ the Electron maintainers.
  • Security: Similar to how you can fix stability bugs by releasing an app update, you can also release security fixes to your application without asking your customer to upgrade their operating system. Even if operating system providers prioritize updates to their built-in browser, we have not seen them reliably update the built-in web views with similar urgency. Bundling a web renderer gives you, the developer, control.
  • Performance: For simple HTML documents, a built-in web view will sometimes use fewer resources than an app with a bundled framework. For bigger apps, it is our experience that we can deliver better performance with the latest version of Chromium than we can with built-in web views. You might think that the built-in view can share a lot of resources with other apps and the operating system— but for security reasons, apps have to run in their own sandboxes, isolated from each other. At that point, the question is whether the OS’ web view is more performant than Chromium. Across many apps, our experience is that bundling Chromium and Node.js enables us to build better and more performant experiences.

Why bundle Chromium and Node.js

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.

  • Native code when you want it: Thanks to Node.js’ mature native addon system, you can always write native code. There is no system API out of reach for you. Whatever macOS, Windows, or Linux feature you’ll want to integrate with —as long as you can do it in C, C++, Objective-C, Rust, or another native language, you’ll be able to do it in Electron. Again, this gives you, the developer, maximum control. With Electron, you can use web technologies without choosing only web technologies.

Developer experience

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:

  • Powerful ecosystem: Anything you find on npm will run inside Electron. Any resource available to you about how to work with Node.js also applies to Electron. In addition, Electron itself has a thriving ecosystem — including plenty of choices for installers, updaters, deeper operating system-integration, and more.
  • Plenty of built-in capabilities: Over the last ten years, Electron’s core has gained plenty of native capabilities that you might need to build your application. Written in C++ and Objective-C, Electron has dozens of easy-to-use APIs for deeper operating-system integration — like advanced window customization for transparent or oddly shaped widgets, receiving push notifications from the Apple Push Notification Network, or handling a custom URL protocol for your app.
  • Open source: The entire stack is open source and open to your inspection. This ensures your freedom to add any feature or fix any bug you might encounter in the future.
  • Native code when you need it: It bears repeating that Electron allows you to mix and match web technologies and C++, C, Objective-C, Rust, and other native languages. Whether it be SQLite, a whole LLM, or just the ability to call one specific native API, Electron will make it easy.

Why choose something else

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:

Resource-Constrained Environments and IoT: In scenarios with very limited memory or processing power (say, one megabyte of memory and 100MHz of processing power on a low-powered ARM Cortex-M), you will likely need to use a low-level language to directly talk to the display to output basic text and images. Even on slightly higher-powered single-chip devices you might want to consider an embedded UI framework. A classic example is a smart watch.

Small Disk Footprint: Zipped Electron apps are usually around 80 to 100 Megabytes. If a smaller disk footprint is a hard requirement, you’ll have to use something else.

Operating System UI Frameworks and Libraries: By allowing you to write native code, Electron can do anything a native application can do, including the use of the operating system’s UI components, like WinUI, SwiftUI, or AppKit. In practice, most Electron apps make rare use of that ability. If you want the majority of your app to be built with operating system-provided interface components, you’ll likely be better off building fully native apps for each operating system you’d like to target. It’s not that it’s impossible with Electron, it’ll just likely be an overall easier development process.

Games and Real-Time Graphics: If you're building a high-performance game or application requiring complex real-time 3D graphics, native frameworks like Unity, Unreal Engine, or DirectX/OpenGL will provide better performance and more direct access to graphics hardware. Web fans might point out caveats, like the fact that even Unreal Engine ships with Chromium — or that WebGPU and WebGL are developing rapidly and many game engines, including the ones listed here, can now output their games in a format that runs in a browser. That said, if you asked us to build the next AAA game, we’d likely use something else than just web technologies.

Embedding Lightweight Websites: Electron apps typically are mostly web apps with native code sprinkled in where useful. Processing-heavy Electron applications tend to write the UI in HTML/CSS and build the backend in Rust, C++, or another native language. If you’re planning to build a primarily native application that also wants to display a little website in a specific view, you might be better off using the OS-provided web view or something like ultralight.