环境变量

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

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

POSIX shell示例:

  1. $ export ELECTRON_ENABLE_LOGGING=true
  2. $ electron

Windows 控制台示例:

  1. > set ELECTRON_ENABLE_LOGGING=true
  2. > electron

生产环境相关变量

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

NODE_OPTIONS

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

示例:

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

不支持的选项是:

  1. --use-bundled-ca
  2. --force-fips
  3. --enable-fips
  4. --openssl-config
  5. --use-openssl-ca

NODE_OPTIONS are explicitly disallowed in packaged apps, except for the following:

  1. --max-http-header-size
  2. --http-parser

GOOGLE_API_KEY

Geolocation support in Electron requires the use of Google Cloud Platform’s geolocation webservice. To enable this feature, acquire a Google API key and place the following code in your main process file, before opening any browser windows that will make geolocation requests:

  1. process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE'

By default, a newly generated Google API key may not be allowed to make geolocation requests. To enable the geolocation webservice for your project, enable it through the API library.

N.B. You will need to add a Billing Account to the project associated to the API key for the geolocation webservice to work.

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进程启动。

In this mode, you will be able to pass cli options to Node.js as you would when running the normal Node.js executable, with the exception of the following flags:

  • “—openssl-config”
  • “—use-bundled-ca”
  • “—use-openssl-ca”,
  • “—force-fips”
  • “—enable-fips”

These flags are disabled owing to the fact that Electron uses BoringSSL instead of OpenSSL when building Node.js’ crypto module, and so will not work as designed.

ELECTRON_NO_ATTACH_CONSOLE Windows

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

ELECTRON_FORCE_WINDOW_MENU_BAR Linux

不使用 Linux 的全局菜单栏。

ELECTRON_TRASH Linux

Set the trash implementation on Linux. 默认值为 gio.

选项:

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

开发环境相关变量

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

ELECTRON_ENABLE_LOGGING

Prints Chromium’s internal logging to the console.

Setting this variable is the same as passing --enable-logging on the command line. For more info, see --enable-logging in command-line switches.

ELECTRON_LOG_FILE

Sets the file destination for Chromium’s internal logging.

Setting this variable is the same as passing --log-file on the command line. For more info, see --log-file in command-line switches.

ELECTRON_DEBUG_DRAG_REGIONS

Adds coloration to draggable regions on BrowserViews on macOS - draggable regions will be colored green and non-draggable regions will be colored red to aid debugging.

ELECTRON_DEBUG_NOTIFICATIONS

Adds extra logs to Notification lifecycles on macOS to aid in debugging. Extra logging will be displayed when new Notifications are created or activated. They will also be displayed when common a tions are taken: a notification is shown, dismissed, its button is clicked, or it is replied to.

Sample output:

  1. Notification created (com.github.Electron:notification:EAF7B87C-A113-43D7-8E76-F88EC9D73D44)
  2. Notification displayed (com.github.Electron:notification:EAF7B87C-A113-43D7-8E76-F88EC9D73D44)
  3. Notification activated (com.github.Electron:notification:EAF7B87C-A113-43D7-8E76-F88EC9D73D44)
  4. Notification replied to (com.github.Electron:notification:EAF7B87C-A113-43D7-8E76-F88EC9D73D44)

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_DEFAULT_ERROR_MODE Windows

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

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

ELECTRON_OVERRIDE_DIST_PATH

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

  1. export ELECTRON_OVERRIDE_DIST_PATH=/Users/username/projects/electron/out/Testing

Set By Electron

Electron sets some variables in your environment at runtime.

ORIGINAL_XDG_CURRENT_DESKTOP

This variable is set to the value of XDG_CURRENT_DESKTOP that your application originally launched with. Electron 有时修改 XDG_CURRENT_DESKTOP 的值以影响Chromium 中的其他逻辑,所以如果您想访问 原始的 值 您应该重新查看此环境变量。