Open In App

Environment Variables in ElectronJS

ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. 

We have already discussed Command Line Arguments in ElectronJS. Command-line arguments are important because they can be used to control the behaviour of the application. We can pass Command-line arguments to Electron from outside the application while launching or we can simply hard code these values within the application. Environment Variables in Electron also control the application configuration and behaviour without changing the code. Some specific behaviours in Electron are controlled by environment variables rather than command-line arguments because they are initialized earlier than the command-line flags and the application’s main function code. In this tutorial, we will look at the different environment variables used by Electron and their respective classifications. 



We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system. 

As mentioned above, in case we are hardcoding command-line flags within the application, we need to import the CommandLine Property of the app Module, but we don’t need any additional code changes for setting environment variables in Electron. However, Since Electron also supports the global process object, we can set the environment variables via code as well. For example:



process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE'

For more details on the process object, follow Process Object in ElectronJS. There are two main kinds of Environment Variables provided by Electron: 

Development Environment Variables as the name suggests, are intended primarily for development and debugging purposes within the application. 

Production Environment Variables as the name suggests, are intended primarily for use at runtime in a packaged Electron applications. These Environment variables are especially useful when deploying the packaged Electron application to a different server in a production environment. 

process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE'

Apart from the Production and Development Environment variables, there are some variables which are set by Electron itself in the native System environment at runtime. 

Example usage of Setting Environment Variables for Electron in 

~$ set ELECTRON_ENABLE_LOGGING=true
~$ export ELECTRON_ENABLE_LOGGING=true

Output: 

Note: These environment variables are reset and need to be set again every time we restart the computer. If we want to avoid doing so, we need to add these environment variables with their respective values to the .bashrc files. .bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. We can put any command in that file that we could type in the command prompt, and they will not be reset such as in this case, Electron’s environment variables. 


Article Tags :