Skip to main content

Advanced Installation Instructions高级安装说明

To install prebuilt Electron binaries, use npm. 要安装预构建的Electron二进制文件,请使用npmThe preferred method is to install Electron as a development dependency in your app:首选方法是在应用程序中安装Electron作为开发依赖项:

npm install electron --save-dev

See the Electron versioning doc for info on how to manage Electron versions in your apps.有关如何在应用程序中管理Electron版本的信息,请参阅Electron版本控制文档

Running Electron ad-hoc运行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:如果您手头拮据,不希望在本地项目中使用npm install,您也可以使用与npm捆绑的npx命令运行器来运行Electron ad-hoc:

npx electron .

The above command will run the current working directory with Electron. 上述命令将使用Electron运行当前工作目录。Note that any dependencies in your app will not be installed.请注意,将不会安装应用程序中的任何依赖项。

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:如果要更改下载的体系结构(例如,x64机器上的ia32),可以在npm安装中使用--arch标志,或设置npm_config_arch环境变量:

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:除了更改架构之外,还可以使用--platform标志指定平台(如win32linux等):

npm install --platform=win32 electron

Proxies代理

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:如果需要使用HTTP代理,则需要将ELECTRON_GET_USE_PROXY变量设置为任意值,并根据主机系统的节点版本设置其他环境变量:

Custom Mirrors and Caches自定义镜像和缓存

During installation, the electron module will call out to @electron/get to download prebuilt binaries of Electron for your platform. 在安装过程中,electron模块将调用@electron/get为您的平台下载预构建的Electron二进制文件。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的发布下载页面来实现(https://github.com/electron/electron/releases/tag/v$VERSION,其中$VERSION是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.如果您无法访问GitHub或需要提供自定义构建,可以通过提供镜像或现有缓存目录来实现。

Mirror镜像

You can use environment variables to override the base URL, the path at which to look for Electron binaries, and the binary filename. 您可以使用环境变量覆盖基本URL、查找Electron二进制文件的路径和二进制文件名。The URL used by @electron/get is composed as follows:@electron/get使用的URL组成如下:

url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME

For instance, to use the China CDN mirror:例如,要使用中国CDN镜像:

ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"

By default, ELECTRON_CUSTOM_DIR is set to v$VERSION. 默认情况下,ELECTRON_CUSTOM_DIR设置为v$VERSIONTo change the format, use the {{ version }} placeholder. 要更改格式,请使用{{ version }}占位符。For example, version-{{ version }} resolves to version-5.0.0, {{ version }} resolves to 5.0.0, and v{{ version }} is equivalent to the default. 例如,version-{{ version }}解析为version-5.0.0{{ version }}解析为5.0.0,而v{{ version }}等于默认值。As a more concrete example, to use the China non-CDN mirror:作为一个更具体的例子,使用中国非CDN镜像:

ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
ELECTRON_CUSTOM_DIR="{{ version }}"

The above configuration will download from URLs such as https://npmmirror.com/mirrors/electron/8.0.0/electron-v8.0.0-linux-x64.zip.上述配置将从URL下载,如https://npmmirror.com/mirrors/electron/8.0.0/electron-v8.0.0-linux-x64.zip

If your mirror serves artifacts with different checksums to the official Electron release you may have to set electron_use_remote_checksums=1 to force Electron to use the remote SHASUMS256.txt file to verify the checksum instead of the embedded checksums.如果您的镜像向官方Electron版本提供具有不同校验和的工件,您可能必须将electron_use_remote_checksums设置为1,以强制Electron使用远程SHASUMS256.txt文件来验证校验和,而不是嵌入校验和。

Cache缓存

Alternatively, you can override the local cache. 或者,您可以覆盖本地缓存。@electron/get will cache downloaded binaries in a local directory to not stress your network. @electron/get将下载的二进制文件缓存在本地目录中,以避免对网络造成压力。You can use that cache folder to provide custom builds of Electron or to avoid making contact with the network at all.您可以使用该缓存文件夹提供自定义的Electron版本,或者根本不与网络联系。

  • 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.在使用旧版本的Electron的环境中,您可能会在~/.electron中找到缓存。

You can also override the local cache location by providing a electron_config_cache environment variable.您还可以通过提供electron_config_cache环境变量来覆盖本地缓存位置。

The cache contains the version's official zip file as well as a checksum, and is stored as [checksum]/[filename]. 缓存包含版本的官方zip文件以及校验和,并存储为[checksum]/[filename]A typical cache might look like this:典型的缓存可能如下所示:

├── a91b089b5dc5b1279966511344b805ec84869b6cd60af44f800b363bba25b915
│ └── electron-v15.3.1-darwin-x64.zip

Skip binary download跳过二进制文件下载

Under the hood, Electron's JavaScript API binds to a binary that contains its implementations. 实际上,Electron的JavaScript API绑定到包含其实现的二进制文件。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应用程序的功能都至关重要,因此每次从npm注册表安装Electron程序时,默认情况下都会在postinstall步骤中下载该二进制文件。

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. 但是,如果您想安装项目的依赖项,但不需要使用Electron功能,则可以设置ELECTRON_SKIP_BINARY_DOWNLOAD环境变量,以防止下载二进制文件。For instance, this feature can be useful in continuous integration environments when running unit tests that mock out the electron module.例如,当运行模拟electron模块的单元测试时,此功能在连续集成环境中非常有用。

ELECTRON_SKIP_BINARY_DOWNLOAD=1 npm install

Troubleshooting故障排除

When running npm install electron, some users occasionally encounter installation errors.在运行npm install electron时,一些用户偶尔会遇到安装错误。

In almost all cases, these errors are the result of network problems and not actual issues with the electron npm package. 在几乎所有情况下,这些错误都是网络问题的结果,而不是electronnpm封装的实际问题。Errors like ELIFECYCLE, EAI_AGAIN, ECONNRESET, and ETIMEDOUT are all indications of such network problems. ELIFECYCLEEAI_AGAINECONNRESETETIMEDOUT这样的错误都是此类网络问题的指示。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.如果通过npm安装失败,您也可以尝试直接从electron/electron/releases下载Electron。

If installation fails with an EACCESS error you may need to fix your npm permissions.如果安装失败并出现EACCESS错误,您可能需要修复npm权限

If the above error persists, the unsafe-perm flag may need to be set to true:如果上述错误仍然存在,则可能需要将unsafe-perm标志设置为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:在速度较慢的网络上,建议使用--verbose标志以显示下载进度:

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.如果需要强制重新下载资源和SHASUM文件,请将force_no_cache环境变量设置为true