当前位置: 首页 > 文档资料 > Electron 中文文档 >

安装

优质
小牛编辑
153浏览
2023-12-01

要安装预编译好的的二进制文件, 请使用 npm。 首选的方法是在项目中作为development dependency安装。

npm install electron --save-dev

查看versioning doc获取如何在你的应用中管理Electron的相关信息。

Installation

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

See the Electron versioning doc for info on how to manage Electron versions in your apps.

全局安装

您还可以在 $PATH 中全局安装 electron 命令:

npm install electron -g

Global Installation

You can also install the electron command globally in your $PATH:

npm install electron -g

自定义

如果想修改下载安装的位版本(例如, 在x64机器上安装ia32位版本), 你可以使用npm install中的--arch标记,或者设置npm_config_arch 环境变量:

npm install --arch=ia32 electron

此外, 您还可以使用 --platform 来指定开发平台 (例如, win32linux 等):

npm install --platform=win32 electron

Customization

If you want to change the architecture that is downloaded (e.g., ia32 on an x64 machine), you can use the --arch flag with npm install or set the npm_config_arch environment variable:

npm install --arch=ia32 electron

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

代理

如果需要使用 HTTP 代理, 则可以 设置这些环境变量 。

Proxies

If you need to use an HTTP proxy you can set these environment variables.

自定义镜像和缓存

在安装过程中,electron 模块会通过 electron-download 为您的平台下载 Electron 的预制二进制文件。 这将通过访问 GitHub 的发布下载页面来完成 (https://github.com/electron/electron/releases/tag/v$VERSION, 这里的 $VERSION 是 Electron 的确切版本).

如果您无法访问GitHub,或者您需要提供自定义构建,则可以通过提供镜像或现有的缓存目录来实现。

Custom Mirrors and Caches

During installation, the electron module will call out to electron-download 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).

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.

镜像

您可以使用环境变量来覆盖基本 URL,查找 Electron 二进制文件的路径以及二进制文件名。 使用 electron-download 的网址 组成如下::

url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME

例如,使用中国镜像:

ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"

Mirror

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-download is composed as follows:

url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME

For instance, to use the China mirror:

ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"

缓存

或者,您可以覆盖本地缓存。 electron-download 会将下载的二进制文件缓存在本地目录中,不会增加网络负担。 您可以使用该缓存文件夹来提供 Electron 的定制版本,或者避免进行网络连接。

  • Linux: $XDG_CACHE_HOME or ~/.cache/electron/
  • MacOS: ~/Library/Caches/electron/
  • Windows: $LOCALAPPDATA/electron/Cache or ~/AppData/Local/electron/Cache/

在使用旧版本 Electron 的环境中,您也可以在~/.electron中找到缓存。

您也可以通过提供一个 ELECTRON_CACHE 环境变量来覆盖本地缓存位置。

缓存包含版本的官方zip文件以及校验和,存储为文本文件。 典型的缓存可能如下所示:

├── electron-v1.7.9-darwin-x64.zip
├── electron-v1.8.1-darwin-x64.zip
├── electron-v1.8.2-beta.1-darwin-x64.zip
├── electron-v1.8.2-beta.2-darwin-x64.zip
├── electron-v1.8.2-beta.3-darwin-x64.zip
├── SHASUMS256.txt-1.7.9
├── SHASUMS256.txt-1.8.1
├── SHASUMS256.txt-1.8.2-beta.1
├── SHASUMS256.txt-1.8.2-beta.2
├── SHASUMS256.txt-1.8.2-beta.3

Cache

Alternatively, you can override the local cache. electron-download 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 or ~/.cache/electron/
  • MacOS: ~/Library/Caches/electron/
  • Windows: $LOCALAPPDATA/electron/Cache or ~/AppData/Local/electron/Cache/

On environments that have been using older versions of Electron, you might find the cache also in ~/.electron.

You can also override the local cache location by providing a ELECTRON_CACHE environment variable.

The cache contains the version's official zip file as well as a checksum, stored as a text file. A typical cache might look like this:

├── electron-v1.7.9-darwin-x64.zip
├── electron-v1.8.1-darwin-x64.zip
├── electron-v1.8.2-beta.1-darwin-x64.zip
├── electron-v1.8.2-beta.2-darwin-x64.zip
├── electron-v1.8.2-beta.3-darwin-x64.zip
├── SHASUMS256.txt-1.7.9
├── SHASUMS256.txt-1.8.1
├── SHASUMS256.txt-1.8.2-beta.1
├── SHASUMS256.txt-1.8.2-beta.2
├── SHASUMS256.txt-1.8.2-beta.3

跳过二进制包下载

当您在安装 electron NPM 包时, 它会自动下载 electron 的二进制包。

当在CI环境中 测试另一个组件的时候,这可能是不必要的。

为了防止当您安装所有 npm 依赖关系时下载二进制文件,您可以设置环境变量 ELECTRON_SKIP_BINARY_DOWNODD。 例如:

ELECRON_SKIP_BINARY_DOWNOAD=1 npm install

Skip binary download

When installing the electron NPM package, it automatically downloads the electron binary.

This can sometimes be unnecessary, e.g. in a CI environment, when testing another component.

To prevent the binary from being downloaded when you install all npm dependencies you can set the environment variable ELECTRON_SKIP_BINARY_DOWNLOAD. E.g.:

ELECTRON_SKIP_BINARY_DOWNLOAD=1 npm install

故障排查

在运行 npm install electron 时,有些用户会偶尔遇到安装问题。

在大多数情况下,这些错误都是由网络问题导致,而不是因为 electron npm 包的问题。 如 ELIFECYCLEEAI_AGAINECONNRESETETIMEDOUT 等错误都是此类网络问题的标志。 最佳的解决方法是尝试切换网络,或是稍后再尝试安装。

如果通过 npm 安装失败,您可以尝试直接从 electron/electron/releases 直接下载 Electron。

如果安装失败并出现 EACCESS 错误, 则可能需要 修复您的 npm 权限 。(例如使用 sudo )

如果上述错误仍然存在, 则可能需要将参数 unsafe-perm 设置为 true

sudo npm install electron --unsafe-perm=true

在较慢的网络上, 最好使用 --verbose 标志来显示下载进度:

npm install --verbose electron

如果需要强制重新下载文件, 并且 SHASUM 文件将 force_no_cache 环境变量设置为 true

Troubleshooting

When running npm install electron, some users occasionally encounter installation errors.

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.

You can also attempt to download Electron directly from electron/electron/releases if installing via npm is failing.

If installation fails with an EACCESS error you may need to fix your npm permissions.

If the above error persists, the unsafe-perm flag may need to be set to true:

sudo npm install electron --unsafe-perm=true

On slower networks, it may be advisable to use the --verbose flag in order to show download progress:

npm install --verbose electron

If you need to force a re-download of the asset and the SHASUM file set the force_no_cache environment variable to true.