为什么选择 Electron
¥Why Electron
Electron 是一个框架,使开发者能够为 macOS、Windows 和 Linux 构建跨平台桌面应用通过将 Web 技术(HTML、JavaScript、CSS)与 Node.js 和原生代码相结合。它是开源的、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 的好处分为两个问题:首先,为什么要使用 Web 技术来构建应用?那么,为什么要选择 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?
如果你已经在应用中使用了 Web 技术,则可以直接跳到下面的 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 的实际 任务控制 是使用 Web 技术编写的。彭博终端 是每个金融机构的计算机系统,采用 Web 技术编写并在 Chromium 内部运行。每个用户每年的费用为 25,000 美元。麦当劳订餐亭为世界上最大的食品零售商提供支持,完全由 Chromium 构建。SpaceX 的 Dragon 2 太空舱 使用 Chromium 显示其界面。你明白了:Web 技术是构建用户界面的绝佳技术堆栈。
¥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 使你的开发者和设计人员能够充分表达自己。Web 的展示包括 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. The 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
无论你需要与什么提供商或客户数据交互,他们可能已经想到了与 Web 的集成路径。根据你的技术选择,嵌入 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.
如果你使用 Web 技术构建应用,几乎没有你的应用无法运行的平台。几乎所有带显示屏的设备(无论是 ATM、汽车信息娱乐系统、智能电视、冰箱还是 Nintendo Switch)都带有显示 Web 技术的方法。如果你想跨平台,Web 是安全的选择。
¥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 safe bet if you want to be cross-platform.
普遍性
¥Ubiquity
很容易找到具有使用 Web 技术构建经验的开发者。如果你是开发者,那么在 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 和 Skype 都是使用 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 Skype 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 在同一天。要实现这一点,需要投入大量工作,包括构建流程和工具,以及每周投入的原始时间。
¥Reliable release schedule: Electron will release a new major version in lockstep with every second major Chromium release, usually on the same day as Chromium. A lot of work, both in the form of building processes and tools, but also in terms of raw invested hours every week, has to go into making that happen.
-
没有独裁者:有时,押注一项技术也需要你押注一个人或一家公司。反过来,它要求你相信该人或公司永远不会出现故障,不会直接与你争吵,也不会做任何其他激烈的事情来迫使你重新考虑整个技术堆栈。Electron 由多种公司(Microsoft、Slack/Salesforce、Notion 等)维护,并将继续欢迎更多有兴趣确保其“决策席位”的公司。
¥No dictators: Sometimes, betting on a technology also requires you to bet on a single person or company. In turn, it requires you to trust that the person or company never has a breakdown, starts fighting you directly, or does anything else drastic that’ll force you rethink your entire tech stack. Electron is maintained by a diverse set of companies (Microsoft, Slack/Salesforce, Notion, and more) and will continue to welcome more companies interested in ensuring their “seat at the decision-making table”.
稳定性、安全性、性能
¥Stability, security, performance
Electron 通过将最新版本的 Chromium、V8 和 Node.js 直接与应用二进制文件打包在一起,在所有目标平台(macOS、Windows、Linux)上提供最佳体验。在以最高的稳定性、安全性和性能运行和渲染 Web 内容方面,我们目前认为该堆栈是“同类最佳”。
¥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
你可能想知道,当大多数现代操作系统已经附带浏览器和某种形式的 Web 视图时,为什么我们要将 Chromium 的 Web 堆栈与我们的应用打包在一起。打包不仅会大大增加 Electron 维护者的工作量,还会增加 Electron 应用的总磁盘大小(大多数应用 >100MB)。许多 Electron 维护者曾经开发过使用嵌入式 Web 视图的应用 — — 并且从那时起就接受了增加的磁盘大小和维护者的工作,认为这是值得的权衡。
¥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.
使用操作系统的内置 Web 视图时,你会受到需要支持的最旧操作系统版本中包含的浏览器版本的限制。我们发现这种方法存在以下问题:
¥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:
-
稳定性:现代 Web 技术堆栈非常复杂,因此,你迟早会遇到错误。如果你使用操作系统的 Web 视图,你唯一的办法就是要求你的客户升级他们的操作系统。如果该机器没有可用的升级(因为无法升级到最新的 macOS 或 Windows 11),则必须要求他们购买新电脑。如果你运气不好,那么你现在就会失去一个大客户,因为他们不会因为一个团队想尝试你的初创公司的应用而升级他们整个数千台机器的机群。在这种情况下,你没有追索权。即使发生这种情况的风险对于雇用 Electron 维护者的公司来说也是不可接受的。
¥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.
-
安全性:与通过发布应用更新来修复稳定性错误的方式类似,你也可以发布应用的安全修复程序,而无需要求客户升级其操作系统。即使操作系统提供商优先更新其内置浏览器,我们也未看到他们以类似的紧迫性可靠地更新内置 Web 视图。打包 Web 渲染器可让你(开发者)获得控制权。
¥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.
-
性能:对于简单的 HTML 文档,内置 Web 视图有时会比打包框架的应用使用更少的资源。对于更大的应用,根据我们的经验,与内置 Web 视图相比,使用最新版本的 Chromium 可以提供更好的性能。你可能认为内置视图可以与其他应用和操作系统共享大量资源 — 但出于安全原因,应用必须在自己的沙箱中运行,彼此隔离。此时,问题是操作系统的 Web 视图是否比 Chromium 更高效。在许多应用中,我们的经验是,打包 Chromium 和 Node.js 使我们能够构建更好、更高效的体验。
¥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.
为什么打包 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,你可以使用 Web 技术,而不必只选择 Web 技术。
¥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
总而言之,我们的目标是构建一个成熟、企业级且可用于关键任务应用的 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 本身有一个 蓬勃发展的生态系统 — 包括安装程序、更新程序、更深层次的操作系统集成等的大量选择。
¥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.
-
大量内置功能:在过去十年中,Electron 的核心已经获得了许多你可能需要构建应用的原生功能。Electron 用 C++ 和 Objective-C 编写,具有 数十个易于使用的 API,用于更深入的操作系统集成 — 例如,针对透明或奇形怪状的小部件进行高级窗口自定义、从 Apple Push Notification Network 接收推送通知或为你的应用处理自定义 URL 协议。
¥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.
-
在你需要时提供原生代码:值得重复的是,Electron 允许你混合搭配 Web 技术以及 C++、C、Objective-C、Rust 和其他原生语言。无论是 SQLite、整个 LLM,还是只是调用一个特定原生 API 的能力,Electron 都会让一切变得简单。
¥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
如上所述,Web 是构建界面的绝佳平台。这并不意味着我们维护者会使用 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 上的 1 兆字节内存和 100MHz 处理能力),你可能需要使用底层语言直接与显示器对话以输出基本文本和图片。即使在功率稍高的单芯片设备上,你可能也需要考虑嵌入式 UI 框架。一个典型的例子是智能监视。
¥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.
小磁盘占用:压缩的 Electron 应用通常约为 80 到 100 兆字节。如果较小的磁盘占用空间是硬性要求,则必须使用其他东西。
¥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.
操作系统 UI 框架和库:通过允许你编写原生代码,Electron 可以执行原生应用可以执行的任何操作,包括使用操作系统的 UI 组件,如 WinUI、SwiftUI 或 AppKit。实际上,大多数 Electron 应用很少使用该功能。如果你希望大多数应用都使用操作系统提供的界面组件构建,那么为你想要定位的每个操作系统构建完全原生的应用可能会更好。使用 Electron 并非不可能,只是它可能是一个总体上更简单的开发过程。
¥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.
游戏和实时图形:如果你正在构建需要复杂实时 3D 图形的高性能游戏或应用,那么 Unity、Unreal Engine 或 DirectX/OpenGL 等原生框架将提供更好的性能和更直接的图形硬件访问。Web 爱好者可能会指出一些注意事项,例如,即使 Unreal Engine 也附带 Chromium — 或者 WebGPU 和 WebGL 正在快速发展,许多游戏引擎(包括此处列出的引擎)现在可以以在浏览器中运行的格式输出其游戏。也就是说,如果你要求我们构建下一个 AAA 游戏,我们可能会使用除 Web 技术以外的其他技术。
¥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.
嵌入轻量级网站:Electron 应用通常主要是 Web 应用,在有用的地方会使用原生代码。处理量大的 Electron 应用倾向于用 HTML/CSS 编写 UI,并用 Rust、C++ 或其他原生语言构建后端。如果你计划构建一个主要为原生的应用,并且还希望在特定视图中显示一个小网站,那么最好使用操作系统提供的 Web 视图或类似 ultralight 的东西。
¥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.