Open In App

Hot Reload 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.

Several powerful frameworks such as Angular, AngularJS 1.x, React, React-Native, etc implement a tool like Webpack. Webpack is a static module bundler for modern JavaScript applications. It is a powerful tool that provides utilities to make the development process faster and better. One of the most useful features provided by webpack is Hot Reloading Capability. Hot Reloading capability lets the developer modify project source code to instantly reflect changes in the output/browser without updating the entire state of the application. The electron does not provide any in-built hot reloading module however, we can still implement hot reloading capability using open-source packages. This tutorial will demonstrate how to implement hot reloading in Electron using electron-reload npm package and electron-reloader npm package.



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.

Example: We will start by building the basic Electron Application by following the given steps.



Hot Reload in Electron: Hot Reloading should only be implemented in the development environment. Therefore we need to have control over the application environment before implementing this feature. NodeJS provides us with a way by which we can control the application environment via code using Environment Variables.

main.js: Add the following snippet in that file.

const env = process.env.NODE_ENV || 'development';

The NODE_ENV is an Environment Variable which stands for node environment in NodeJS. When a Node application is launched, it can check this environment variable and based on the value perform additional tasks and code logic. According to convention there should be only two values defined for the NODE_ENV variable i.e. either production or development however we can define as many values as required. We can define additional value such as test to execute automated test cases within the application. If we have not set the environment variable and have not explicitly defined the value within code then it will default to undefined.

Note: The NODE_ENV variable will be reset on System Reboot if set using the above methods. To persist this value in the System, set it in the System Environment Variables via Control Panel in Windows. We can also set NODE_ENV on application startup from package.json by updating the start script.

In the code, we have set the NODE_ENV to development. We can implement Hot Reload in Electron by following any of the two approaches:


Article Tags :