Electron - Environment Variables
Electron - Environment Variables
Section titled “Electron - Environment Variables”Environment variables are a powerful way to control an application’s configuration and behavior without modifying its code. Some Electron behaviors are managed by environment variables because they are initialized before command-line flags or the app’s code can take effect.
Electron recognizes several environment variables, broadly categorized into those for production and those for development/debugging.
Production Variables
Section titled “Production Variables”These variables are primarily intended for use at runtime in packaged Electron applications.
| Variable | Description |
|---|---|
ELECTRON_NO_ASAR | When set to true, disables ASAR archive support. Useful for debugging issues related to ASAR packaging. Generally, ASAR archives improve performance by concatenating files. |
ELECTRON_RUN_AS_NODE | When set, Electron starts the process as a normal Node.js process, bypassing the typical Electron app lifecycle. This is useful for running utility scripts or tests that need Node.js APIs provided by Electron’s bundled Node. |
GOOGLE_API_KEY | Electron uses Google’s geocoding web service for certain location APIs. If your app makes frequent geocoding requests, the default shared API key might hit rate limits. Set this variable to your own Google API key in your main process file before any windows requiring geocoding are opened: process.env.GOOGLE_API_KEY = 'YOUR_OWN_API_KEY'; |
ELECTRON_FORCE_WINDOW_MENU_BAR (Linux Only) | On some Linux distributions, Electron might use a global menu bar by default. Setting this variable (e.g., ELECTRON_FORCE_WINDOW_MENU_BAR=1) forces Electron to use the window’s own menu bar instead of the global one. |
Development Variables
Section titled “Development Variables”These variables are mainly for development and debugging purposes.
| Variable | Description |
|---|---|
ELECTRON_ENABLE_LOGGING | When set (e.g., ELECTRON_ENABLE_LOGGING=true), prints Chrome’s internal logging messages to the console. This can be very verbose but helpful for diagnosing low-level issues. |
ELECTRON_ENABLE_STACK_DUMPING | When set (e.g., ELECTRON_ENABLE_STACK_DUMPING=true), Electron will print a stack trace to the console if the application crashes. This is useful for identifying the source of native crashes. |
ELECTRON_DISABLE_SANDBOX (Use with caution) | When set to true, this disables the Chromium sandbox for all processes, including renderer processes. This can be useful for certain debugging scenarios or if you need to use Node.js features directly in a renderer process without a preload script, but it significantly reduces security. It’s generally recommended to keep the sandbox enabled and use preload scripts for Node.js access. |
ELECTRON_DEFAULT_ERROR_MODE (Windows Only) | On Windows, setting this variable (e.g., ELECTRON_DEFAULT_ERROR_MODE=true) shows the Windows crash dialog when Electron crashes, which might provide more system-level information about the crash. |
Setting Environment Variables
Section titled “Setting Environment Variables”How you set environment variables depends on your operating system and shell.
For Windows (Command Prompt)
Section titled “For Windows (Command Prompt)”set ELECTRON_ENABLE_LOGGING=trueREM Now run your electron app from this same terminalFor Windows (PowerShell)
Section titled “For Windows (PowerShell)”$env:ELECTRON_ENABLE_LOGGING="true"# Now run your electron app from this same terminalFor Linux / macOS (bash/zsh)
Section titled “For Linux / macOS (bash/zsh)”export ELECTRON_ENABLE_LOGGING=true# Now run your electron app from this same terminalThese settings are typically for the current terminal session only. To make them permanent, you’d add them to your shell’s profile file (e.g., .bashrc, .zshrc, or Environment Variables settings in Windows System Properties).
For cross-platform consistency in package.json scripts, consider using a utility like cross-env:
// First, install it: npm install --save-dev cross-env// Then, in package.json scripts:"scripts": { "start-debug": "cross-env ELECTRON_ENABLE_LOGGING=true electron ."}