高级安装说明
🌐 Advanced Installation Instructions
要安装预构建的 Electron 二进制文件,请使用 npm。推荐的方法是在你的应用中将 Electron 作为开发依赖安装:
🌐 To install prebuilt Electron binaries, use npm.
The preferred method is to install Electron as a development dependency in your
app:
npm install electron --save-dev
请参阅 Electron 版本说明文档 以获取关于如何在应用中管理 Electron 版本的信息。
🌐 See the Electron versioning doc for info on how to manage Electron versions in your apps.
临时运行 Electron
🌐 Running Electron ad-hoc
如果你处于紧急情况,并且希望在本地项目中不使用 npm install,你也可以使用随 npm 一起打包的 npx 命令运行器临时运行 Electron:
🌐 If you're in a pinch and would prefer to not use npm install in your local
project, you can also run Electron ad-hoc using the npx command runner
bundled with npm:
npx electron .
上述命令将使用 Electron 运行当前工作目录。请注意,你的应用中的任何依赖都不会被安装。
🌐 The above command will run the current working directory with Electron. Note that any dependencies in your app will not be installed.
定制化
🌐 Customization
如果你想更改下载的架构(例如,在 arm64 机器上使用 x64),你可以在 npm install 时使用 --arch 标志,或者设置 npm_config_arch 环境变量:
🌐 If you want to change the architecture that is downloaded (e.g., x64 on an
arm64 machine), you can use the --arch flag with npm install or set the
npm_config_arch environment variable:
npm install --arch=x64 electron
除了更改架构之外,你还可以使用 --platform 标志指定平台(例如,win32、linux 等):
🌐 In addition to changing the architecture, you can also specify the platform
(e.g., win32, linux, etc.) using the --platform flag:
npm install --platform=win32 electron
代理
🌐 Proxies
如果你需要使用 HTTP 代理,你需要将 ELECTRON_GET_USE_PROXY 变量设置为任意值,并根据你的主机系统的 Node 版本设置其他环境变量:
🌐 If you need to use an HTTP proxy, you need to set the ELECTRON_GET_USE_PROXY variable to any
value, plus additional environment variables depending on your host system's Node version:
自定义镜像和缓存
🌐 Custom Mirrors and Caches
在安装进程中,electron 模块将调用 @electron/get 来下载适用于你平台的 Electron 预构建二进制文件。它将通过访问 GitHub 的发布下载页面 (https://github.com/electron/electron/releases/tag/v$VERSION,其中 $VERSION 是 Electron 的确切版本) 来实现这一点。
🌐 During installation, the electron module will call out to
@electron/get to download prebuilt binaries of
Electron for your platform. It will do so by contacting GitHub's
release download page (https://github.com/electron/electron/releases/tag/v$VERSION,
where $VERSION is the exact version of Electron).
如果你无法访问 GitHub 或需要提供自定义构建,你可以通过提供镜像或现有的缓存目录来实现。
🌐 If you are unable to access GitHub or you need to provide a custom build, you can do so by either providing a mirror or an existing cache directory.
镜像
🌐 Mirror
你可以使用环境变量来覆盖基础 URL、查找 Electron 二进制文件的路径以及二进制文件名称。@electron/get 使用的 URL 组成如下:
🌐 You can use environment variables to override the base URL, the path at which to
look for Electron binaries, and the binary filename. The URL used by @electron/get
is composed as follows:
url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME
以使用中国 CDN 镜像为例:
🌐 For instance, to use the China CDN mirror:
ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
默认情况下,ELECTRON_CUSTOM_DIR 设置为 v$VERSION。要更改格式,可使用 {{ version }} 占位符。例如,version-{{ version }} 对应 version-5.0.0,{{ version }} 对应 5.0.0,而 v{{ version }} 与默认设置相同。更具体的例子是,要使用中国非 CDN 镜像:
🌐 By default, ELECTRON_CUSTOM_DIR is set to v$VERSION. To change the format,
use the {{ version }} placeholder. For example, version-{{ version }}
resolves to version-5.0.0, {{ version }} resolves to 5.0.0, and
v{{ version }} is equivalent to the default. As a more concrete example, to
use the China non-CDN mirror:
ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
ELECTRON_CUSTOM_DIR="{{ version }}"
上述配置将从类似 https://npmmirror.com/mirrors/electron/8.0.0/electron-v8.0.0-linux-x64.zip 的 URL 下载。
🌐 The above configuration will download from URLs such as
https://npmmirror.com/mirrors/electron/8.0.0/electron-v8.0.0-linux-x64.zip.
如果你的镜像提供的文件与官方 Electron 版本的校验和不同,你可能需要直接设置 electron_use_remote_checksums=1,或者在 .npmrc 文件中进行配置,以强制 Electron 使用远程的 SHASUMS256.txt 文件来验证校验和,而不是使用嵌入的校验和。
🌐 If your mirror serves artifacts with different checksums to the official
Electron release you may have to set electron_use_remote_checksums=1 directly,
or configure it in a .npmrc file, to force Electron to use the remote SHASUMS256.txt
file to verify the checksum instead of the embedded checksums.
缓存
🌐 Cache
或者,你可以覆盖本地缓存。@electron/get 会将下载的二进制文件缓存到本地目录,以减少网络压力。你可以使用该缓存文件夹提供自定义构建的 Electron,或完全避免与网络连接。
🌐 Alternatively, you can override the local cache. @electron/get will cache
downloaded binaries in a local directory to not stress your network. You can use
that cache folder to provide custom builds of Electron or to avoid making contact
with the network at all.
- Linux:
$XDG_CACHE_HOME或~/.cache/electron/ - macOS:
~/Library/Caches/electron/ - Windows:
$LOCALAPPDATA/electron/Cache或~/AppData/Local/electron/Cache/
在使用较旧版本 Electron 的环境中,你可能会在 ~/.electron 中找到缓存。
🌐 On environments that have been using older versions of Electron, you might find the
cache also in ~/.electron.
你也可以通过提供一个 electron_config_cache 环境变量来覆盖本地缓存位置。
🌐 You can also override the local cache location by providing a electron_config_cache
environment variable.
缓存包含该版本的官方压缩文件以及校验和,并存储为 [checksum]/[filename]。一个典型的缓存可能如下所示:
🌐 The cache contains the version's official zip file as well as a checksum, and is stored as
[checksum]/[filename]. A typical cache might look like this:
├── a91b089b5dc5b1279966511344b805ec84869b6cd60af44f800b363bba25b915
│ └── electron-v15.3.1-darwin-x64.zip
安装后脚本
🌐 Postinstall script
在底层,Electron 的 JavaScript API 绑定到包含其实现的二进制文件。由于这个二进制文件对任何 Electron 应用的功能至关重要,因此在每次从 npm 注册表安装 electron 时,它会在默认的 postinstall 步骤中下载。
🌐 Under the hood, Electron's JavaScript API binds to a binary that contains its
implementations. Because this binary is crucial to the function of any Electron app,
it is downloaded by default in the postinstall step every time you install electron
from the npm registry.
但是,如果你想安装项目的依赖但不需要使用 Electron 功能,你可以设置 ELECTRON_SKIP_BINARY_DOWNLOAD 环境变量来防止下载二进制文件。例如,当在持续集成环境中运行模拟 electron 模块的单元测试时,这个功能会非常有用。
🌐 However, if you want to install your project's dependencies but don't need to use
Electron functionality, you can set the ELECTRON_SKIP_BINARY_DOWNLOAD environment
variable to prevent the binary from being downloaded. For instance, this feature can
be useful in continuous integration environments when running unit tests that mock
out the electron module.
- npm
- Yarn
ELECTRON_SKIP_BINARY_DOWNLOAD=1 npm install
ELECTRON_SKIP_BINARY_DOWNLOAD=1 yarn install
故障排除
🌐 Troubleshooting
在运行 npm install electron 时,一些用户偶尔会遇到安装错误。
🌐 When running npm install electron, some users occasionally encounter
installation errors.
几乎在所有情况下,这些错误都是网络问题导致的,而不是 electron npm 包本身的问题。像 ELIFECYCLE、EAI_AGAIN、ECONNRESET 和 ETIMEDOUT 这样的错误都是这类网络问题的表现。最好的解决方法是尝试切换网络,或者稍等一会儿再尝试安装。
🌐 In almost all cases, these errors are the result of network problems and not
actual issues with the electron npm package. Errors like ELIFECYCLE,
EAI_AGAIN, ECONNRESET, and ETIMEDOUT are all indications of such
network problems. The best resolution is to try switching networks, or
wait a bit and try installing again.
如果通过 npm 安装失败,你也可以尝试直接从 electron/electron/发布 下载 Electron。
🌐 You can also attempt to download Electron directly from
electron/electron/releases
if installing via npm is failing.
如果安装因 EACCESS 错误而失败,你可能需要 修复你的 npm 权限。
🌐 If installation fails with an EACCESS error you may need to
fix your npm permissions.
如果上述错误仍然存在,可能需要将 不安全权限 标志设置为 true:
🌐 If the above error persists, the unsafe-perm flag may need to be set to true:
sudo npm install electron --unsafe-perm=true
在较慢的网络环境下,建议使用 --verbose 标志以显示下载进度:
🌐 On slower networks, it may be advisable to use the --verbose flag in order to
show download progress:
npm install --verbose electron
如果你需要强制重新下载该资源和 SHASUM 文件,请将 force_no_cache 环境变量设置为 true。
🌐 If you need to force a re-download of the asset and the SHASUM file set the
force_no_cache environment variable to true.