Skip to main content

Prerequisites先决条件

Electron is a framework for building desktop applications using JavaScript, HTML, and CSS. Electron是一个使用JavaScript、HTML和CSS构建桌面应用程序的框架。By embedding Chromium and Node.js into a single binary file, Electron allows you to create cross-platform apps that work on Windows, macOS, and Linux with a single JavaScript codebase.通过将ChromiumNode.js嵌入到单个二进制文件中,Electron允许您使用单个JavaScript代码库创建在Windows、macOS和Linux上工作的跨平台应用程序。

This tutorial will guide you through the process of developing a desktop application with Electron and distributing it to end users.本教程将指导您使用Electron开发桌面应用程序并将其分发给最终用户。

Assumptions假设

Electron is a native wrapper layer for web apps and is run in a Node.js environment. Electron是web应用程序的本地包装层,在Node.js环境中运行。Therefore, this tutorial assumes you are generally familiar with Node and front-end web development basics. 因此,本教程假设您通常熟悉节点和前端web开发基础知识。If you need to do some background reading before continuing, we recommend the following resources:如果您需要在继续之前进行一些背景阅读,我们建议您参考以下资源:

Required tools所需工具

Code editor代码编辑器

You will need a text editor to write your code. 您需要一个文本编辑器来编写代码。We recommend using Visual Studio Code, although you can choose whichever one you prefer.我们建议使用Visual Studio Code,尽管您可以选择您喜欢的代码。

Command line命令行

Throughout the tutorial, we will ask you to use various command-line interfaces (CLIs). 在整个教程中,我们将要求您使用各种命令行界面(CLI)。You can type these commands into your system's default terminal:您可以在系统的默认终端中键入以下命令:

  • Windows: Command Prompt or PowerShell命令提示符或PowerShell
  • macOS: Terminal终端
  • Linux: varies depending on distribution (e.g. GNOME Terminal, Konsole)根据分布情况而变化(例如GNOME终端、Konsole)

Most code editors also come with an integrated terminal, which you can also use.大多数代码编辑器还带有一个集成终端,您也可以使用它。

Git and GitHub

Git is a commonly-used version control system for source code, and GitHub is a collaborative development platform built on top of it. Git是一个常用的源代码版本控制系统,GitHub是一个基于它的协作开发平台。Although neither is strictly necessary to building an Electron application, we will use GitHub releases to set up automatic updates later on in the tutorial. 虽然这两个版本都不是构建Electron应用程序所必需的,但我们将在本教程后面使用GitHub版本设置自动更新。Therefore, we'll require you to:因此,我们要求您:

If you're unfamiliar with how Git works, we recommend reading GitHub's Git guides. 如果您不熟悉Git的工作原理,我们建议您阅读GitHub的Git指南You can also use the GitHub Desktop app if you prefer using a visual interface over the command line.如果您喜欢在命令行上使用可视化界面,也可以使用GitHub桌面应用程序。

We recommend that you create a local Git repository and publish it to GitHub before starting the tutorial, and commit your code after every step.我们建议您在开始本教程之前创建一个本地Git存储库并将其发布到GitHub,并在每个步骤之后提交代码。

Installing Git via GitHub Desktop通过GitHub桌面安装Git

GitHub Desktop will install the latest version of Git on your system if you don't already have it installed.GitHub Desktop将在您的系统上安装最新版本的Git(如果尚未安装)。

Node.js and npm

To begin developing an Electron app, you need to install the Node.js runtime and its bundled npm package manager onto your system. 要开始开发Electron应用程序,您需要在系统上安装Node.js运行时及其捆绑的npm包管理器。We recommend that you use the latest long-term support (LTS) version.我们建议您使用最新的长期支持(LTS)版本。

tip

Please install Node.js using pre-built installers for your platform. 请使用您平台的预构建安装程序安装Node.js。You may encounter incompatibility issues with different development tools otherwise. 否则,您可能会遇到不同开发工具的不兼容性问题。If you are using macOS, we recommend using a package manager like Homebrew or nvm to avoid any directory permission issues.如果您使用的是macOS,我们建议使用Homebrewnvm之类的软件包管理器,以避免任何目录权限问题。

To check that Node.js was installed correctly, you can use the -v flag when running the node and npm commands. 要检查Node.js是否正确安装,可以在运行nodenpm命令时使用-v标志。These should print out the installed versions.这些应打印出已安装的版本。

$ node -v
v16.14.2
$ npm -v
8.7.0
caution

Although you need Node.js installed locally to scaffold an Electron project, Electron does not use your system's Node.js installation to run its code. 尽管您需要在本地安装Node.js来构建一个Electron项目,但Electron并不使用系统的Node.js安装来运行其代码Instead, it comes bundled with its own Node.js runtime. 相反,它与自己的Node.js运行时捆绑在一起。This means that your end users do not need to install Node.js themselves as a prerequisite to running your app.这意味着您的最终用户不需要自己安装Node.js作为运行应用程序的先决条件。

To check which version of Node.js is running in your app, you can access the global process.versions variable in the main process or preload script. 要检查应用程序中运行的Node.js版本,可以访问主进程或预加载脚本中的全局process.versions变量。You can also reference the list of versions in the electron/releases repository.您还可以参考electron/releases存储库中的版本列表。