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

环境变量

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

在不更改代码的情况下控制应用程序配置和行为。

Electrond的某些行为受环境变量的控制, 因为它们比命令行标志和应用程序的代码更早初始化。

POSIX shell示例:

$ export ELECTRON_ENABLE_LOGGING=true
$ electron

Windows 控制台示例:

> set ELECTRON_ENABLE_LOGGING=true
> electron

Environment Variables

Control application configuration and behavior without changing code.

Certain Electron behaviors are controlled by environment variables because they are initialized earlier than the command line flags and the app's code.

POSIX shell example:

$ export ELECTRON_ENABLE_LOGGING=true
$ electron

Windows console example:

> set ELECTRON_ENABLE_LOGGING=true
> electron

生产环境相关变量

以下环境变量主要用于在打包后的Electron应用运行时使用。

Production Variables

The following environment variables are intended primarily for use at runtime in packaged Electron applications.

NODE_OPTIONS

Electron 包括对 Node 的 NODE_OPTIONS 的子集的支持。 除与 Chromium 使用 BoringSSL 相抵触的情况外,大多数都得到了支持。

示例:

export NODE_OPTIONS="--no-warnings --max-old-space-size=2048"

不支持的选项是:

--use-bundled-ca
--force-fips
--enable-fips
--openssl-config
--use-openssl-ca

在打包的应用程序中显式禁止使用 NODE_OPTIONS

NODE_OPTIONS

Electron includes support for a subset of Node's NODE_OPTIONS. The majority are supported with the exception of those which conflict with Chromium's use of BoringSSL.

Example:

export NODE_OPTIONS="--no-warnings --max-old-space-size=2048"

Unsupported options are:

--use-bundled-ca
--force-fips
--enable-fips
--openssl-config
--use-openssl-ca

NODE_OPTIONS are explicitly disallowed in packaged apps.

GOOGLE_API_KEY

You can provide an API key for making requests to Google's geocoding webservice. To do this, place the following code in your main process file, before opening any browser windows that will make geocoding requests:

process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE'

For instructions on how to acquire a Google API key, visit this page. 默认情况下, 可能不允许新生成的 Google API key进行地理编码请求。 若要启用地理编码请求, 请访问 这里 。

GOOGLE_API_KEY

You can provide an API key for making requests to Google's geocoding webservice. To do this, place the following code in your main process file, before opening any browser windows that will make geocoding requests:

process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE'

For instructions on how to acquire a Google API key, visit this page. By default, a newly generated Google API key may not be allowed to make geocoding requests. To enable geocoding requests, visit this page.

ELECTRON_NO_ASAR

禁用 ASAR 支持。仅在设置了 ELECTRON_RUN_AS_NODE变量的fork或spawn的子进程中支持此变量。

ELECTRON_NO_ASAR

Disables ASAR support. This variable is only supported in forked child processes and spawned child processes that set ELECTRON_RUN_AS_NODE.

ELECTRON_RUN_AS_NODE

当做普通Node.js进程启动。

ELECTRON_RUN_AS_NODE

Starts the process as a normal Node.js process.

ELECTRON_NO_ATTACH_CONSOLE Windows

不附加到当前控制台会话。

ELECTRON_NO_ATTACH_CONSOLE Windows

Don't attach to the current console session.

ELECTRON_FORCE_WINDOW_MENU_BAR Linux

不使用 Linux 的全局菜单栏。

ELECTRON_FORCE_WINDOW_MENU_BAR Linux

Don't use the global menu bar on Linux.

ELECTRON_TRASH Linux

在Linux中设置回收站的实现,默认为gio

Options:

  • gvfs-trash
  • trash-cli
  • kioclient5
  • kioclient

ELECTRON_TRASH Linux

Set the trash implementation on Linux. Default is gio.

Options:

  • gvfs-trash
  • trash-cli
  • kioclient5
  • kioclient

开发环境相关变量

以下环境变量主要用于开发和调试目的。

Development Variables

The following environment variables are intended primarily for development and debugging purposes.

ELECTRON_ENABLE_LOGGING

将 Chrome 的内部日志打印到控制台。

ELECTRON_ENABLE_LOGGING

Prints Chrome's internal logging to the console.

ELECTRON_LOG_ASAR_READS

当Electron读取ASAR 文件时, 将读取偏移量和文件路径记录到系统 tmpdir。生成的文件可以提供给 ASAR 模块以优化文件排序。

ELECTRON_LOG_ASAR_READS

When Electron reads from an ASAR file, log the read offset and file path to the system tmpdir. The resulting file can be provided to the ASAR module to optimize file ordering.

ELECTRON_ENABLE_STACK_DUMPING

当Electron崩溃时, 将跟踪堆栈输出到控制台。

如果 crashReporter已经启动了, 则此环境变量将不起作用。

ELECTRON_ENABLE_STACK_DUMPING

Prints the stack trace to the console when Electron crashes.

This environment variable will not work if the crashReporter is started.

ELECTRON_DEFAULT_ERROR_MODE Windows

当Electron崩溃时显示 Windows 的崩溃对话框。

如果 crashReporter已经启动了, 则此环境变量将不起作用。

ELECTRON_DEFAULT_ERROR_MODE Windows

Shows the Windows's crash dialog when Electron crashes.

This environment variable will not work if the crashReporter is started.

ELECTRON_OVERRIDE_DIST_PATH

electron 包运行时,该变量告知 electron命令使用指定构件代替由npm install下载的构件。 用法:

export ELECTRON_OVERRIDE_DIST_PATH=/Users/username/projects/electron/out/Debug

ELECTRON_OVERRIDE_DIST_PATH

When running from the electron package, this variable tells the electron command to use the specified build of Electron instead of the one downloaded by npm install. Usage:

export ELECTRON_OVERRIDE_DIST_PATH=/Users/username/projects/electron/out/Debug