Can HTML, CSS and Javascript really be used to build Desktop Applications?
The answer is yes. 😄
In this Article we will be focussing mainly on how Electron can be used to create desktop applications with Web Technologies like HTML, CSS and Javascript.
Electron
Electron can be used to build Desktop Apps with HTML, CSS and Javascript. Also these apps work for multiple platforms like Windows, Mac, Linux and so on.
Electron combines Chromium and NodeJS into a single runtime. This enables us to run the HTML, CSS and Javascript code as a desktop application.
Electron Forge
If Electron is used directly, then some manual setup is needed before building your application. Also if you want to use Angular, React, Vue or any other framework or library, you will need to manually configure for that.
It provides template applications with Angular, React, Vue and other frameworks which avoids the extra manual setups.
Also it provides an easy way to build and package the application. It also provides many other features which can be found in their documenation.
Pre-requisites
Ensure you have NodeJS installed. It can be installed from here.
Install Electron Forge globally using the following command:
npm install -g electron-forge
Let’s get started with the application
Use the following command to create your application:
electron-forge init simple-desktop-app-electronjs
simple-desktop-app-electronjs is the name of the application.
The above command will take some time to run.
Once it finishes running, start the application using the following commands:
cd simple-desktop-app-electronjs
npm start
This should open up a window like the one shown below:
Understanding the Existing Folder Structure and Code
The application has a particular folder structure. Here I will be mentioning some of the important things in this folder structure.
package.json
It has information about the application you are creating, all the dependencies needed for the app, and some scripts. Some of the scripts are already pre configured, and you can add new scripts as well.
The config.forge path has all the configurations which are specific to ElectronJS. For example make-targets is used to specify the target make files for various platforms like Windows, Mac or Linux.
Also package.json has "main": "src/index.js" which indicates that src/index.js is the starting point of the application
src/index.js
According to package.json, index.js is the main script. The process which runs the main script is known as the main process. So the main process runs the index.js script.
The main process is used to display GUI elements. It does this by creating web pages.
Each web page created runs in a process called the renderer process.
Main process and renderer process
The purpose of the main process is to create web pages using a BrowserWindow Instance.
The BrowserWindow Instance uses a renderer process to run each web page.
Each app can have only one main process but can have many renderer processes.
It is possible to communicate between the main and the renderer process as well. This, however, will not be covered in this article.
Electron Architecture showing main and renderer process. The file names can vary.
abcd.html is shown as a second webpage in the above architecture. But in our code we won’t be having a second web page.
src/index.html
index.js loads the index.html file into a new BrowerWindow Instance.
What this basically means is that index.js creates a new GUI Window, and loads it with index.html web page. The index.html web page runs in its own renderer process.
Code in index.js explained
Most of the code created in index.js has good comments explaining what it does. Here I will mention a few key points to note in index.js:
mainWindow <strong class="markup--strong markup--pre-strong">=</strong> <strong class="markup--strong markup--pre-strong">new</strong> BrowserWindow({
width: 800,
height: 600,
});
<em class="markup--em markup--pre-em">// and load the index.html of the app.</em>
mainWindow.loadURL(`file://${__dirname}/index.html`);
The above code snippet basically creates a BrowserWindow Instance and loads index.html into the BrowserWindow.
You will see app used often in the code. For example take the below code snippet:
app.on('ready', createWindow);
app is used to control the application’s event life cycle.
The above code snippet says that when the application is ready, load the first window.
Similarly, app can be used to perform other actions on various events. For example it can be used to perform some action right before the application closes and so on.
Let’s create a Temperature Converter Desktop Application
Let us use the same application we used before and modify it slightly to create a temperature converter application.
First let us install Bootstrap with the following command:
The celciusToFahrenheit() function reads the value in the Celcius text box, converts it to Fahrenheit, and writes the new temperature into the Fahrenheit text box.
The fahrenheitToCelcius() function does the exact opposite of this.
Running the application
Run the application using the following command:
npm start
This should display the following window. Try it out with different values.
Packaging the application
The command to package the application is:
npm run package
This command will take some time to run. Once it finishes check the out folder within the project folder.
I tested this in a Windows machine. This creates a folder called simple-desktop-app-electronjs-win32-x64 inside the out folder
So in the out/simple-desktop-app-electronjs-win32-x64 folder, the command creates an .exe file for this application. Clicking on the exe file automatically starts the desktop application.
The folder name simple-desktop-app-electronjs-win32-x64 can be broken down as appname-platform-architecture where
appname = simple-desktop-app-electronjs
platform = win32
architecture = x64
When you run this command without any parameters, by default it packages for the platform which you are using for development.
Let’s say you want to package for a different platform and architecture. Then you can use the following syntax:
npm run package -- --platform=<platform> arch=<architecture>
For example, in order to package for linux you can use the following command:
npm run package -- --platform<strong class="markup--strong markup--pre-strong">=</strong>linux --arch<strong class="markup--strong markup--pre-strong">=</strong>x64
This will create a folder called simple-desktop-app-electronjs-linux-x64 inside the out folder.
Creating a make File
In order to create a make file or an installer for the application, use the following command:
npm run make
This command will take some time to run. Once it finishes check the out folder within the project folder.
The out/make folder will have a Windows installer for the desktop application.
When you run this command without any parameters, by default it creates the installer for the platform which you are using for development.
Code
The code for this desktop application is available in my GitHub repo:
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.