Creating an Electron Windows Installer: A Step-by-Step Tutorial

Introduction to Electron and Windows Installer


Electron and Windows Installer logo

As software development continues to evolve, Electron has made a significant contribution to this advancement. Electron is an open-source framework developed by GitHub that allows developers to create cross-platform desktop applications using web technologies such as HTML, CSS, and JavaScript. With Electron, developers can create applications for Windows, Mac, and Linux operating systems with one codebase, making it an efficient option for software creation.

Windows Installer, on the other hand, is a program that facilitates the installation and removal of software in Microsoft Windows operating systems. With Windows Installer, developers can create a user-friendly installation process for their software and ensure that their application is installed correctly on the target machine. Windows Installer saves developers the time and headache of creating their own installation programs from scratch.

Electron and Windows Installer together make a powerful combination for developing efficient and effective software solutions. In this article, we will explore how these two tools work together to create a seamless installation process for your Electron desktop application on the Windows operating system.

Before we dive into the details of creating your Windows Installer for your Electron application, we must first understand how Windows Installer works.

Windows Installer uses an installation package, which contains all the information necessary to install the software on a target machine. The package uses an extensible markup language (XML) schema to define what actions are required to perform the installation. The package uses component, feature, and action data to determine how to install the files and registry settings needed for the software to run correctly. The package also defines the custom actions required to complete the installation successfully. Custom actions may include creating registry settings, launching the application after installation, or creating shortcuts on the desktop or Start menu.

Windows Installer also has a feature called Windows Installer Service, which facilitates the installation of the installation package on the target machine. The service manages the installation process from start to finish, executes custom actions defined in the package, and rolls back the installation if any errors occur during installation.

Now that we have a basic understanding of how Windows Installer works, we can move on to creating an installation package for your Electron application. In the next section, we will explore the steps required to create your Windows Installer using electron-winstaller, a command-line tool for creating Windows Installer packages from Electron applications.

Setting up the Development Environment


setting up development environment

Before you can start creating an Electron application and building the installer, you need to set up your development environment. This includes installing Node.js, npm, and Visual Studio Code.

Node.js

Node.js is a JavaScript runtime that allows you to run JavaScript code outside of a web browser. It’s necessary to have Node.js installed because Electron applications are built using JavaScript. To check if Node.js is installed, open a terminal or command prompt and enter node -v. If you already have Node.js installed, you’ll see the version number. If you don’t have it installed, go to https://nodejs.org/en/download/ and download the latest version for your operating system. Follow the instructions on the website to install Node.js.

npm

npm is the package manager for Node.js. It’s used to install packages and modules that your application needs. To check if npm is installed, open a terminal or command prompt and enter npm -v. If you already have npm installed, you’ll see the version number. If you don’t have it installed, it should be installed with Node.js automatically. You can also check the version of npm that was installed with npm version. To install a package, use the npm install command followed by the name of the package.

Visual Studio Code

Visual Studio Code is a lightweight, cross-platform code editor that’s perfect for developing and debugging Electron applications. It has built-in support for JavaScript, TypeScript, and Node.js. To install Visual Studio Code, go to https://code.visualstudio.com/ and download the latest version for your operating system. Follow the instructions on the website to install Visual Studio Code.

Setting up the project

After you’ve installed Node.js, npm, and Visual Studio Code, you can start setting up your Electron project. To create a new project, open a terminal or command prompt and navigate to the directory where you want to create your project. Use the command npm init to initialize a new Node.js project. This will create a new package.json file in your project directory. The package.json file is used to specify the metadata of your project, such as its name, version, and dependencies.

Installing Electron

Next, you need to install Electron as a development dependency of your project. This can be done using npm. In your terminal or command prompt, navigate to your project directory and use the command npm install --save-dev electron. This will install the latest version of Electron and add it to your package.json file as a development dependency.

Testing the installation

Once you’ve installed Electron, you can test the installation to ensure that everything is working correctly. To do this, create a new file named main.js in your project directory and add the following code:

const { app, BrowserWindow } = require('electron')

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

Save the file and create a new file named index.html in your project directory. Add the following code to the file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

Save the file and return to your terminal or command prompt. Use the command npm start to start the Electron application. This will open a new window that displays the text “Hello World!”. If you see this message, then the installation was successful!

Conclusion

Setting up your development environment is the first step in creating an Electron application and building the installer. By installing Node.js, npm, and Visual Studio Code, you can start developing your application in no time. With Electron, you can build cross-platform desktop applications using your existing web development skillset.

Creating a Windows Installer using Electron Builder


Electron Windows Installer Tutorial

Electron Builder is a tool that allows developers to build cross-platform applications with ease. It simplifies the process of creating installers for Windows, macOS, and Linux. In this tutorial, we will focus on creating a Windows installer using Electron Builder.

Before we dive into the details, let’s make sure that we have the prerequisites installed:

  • Node.js and npm (version 10 and above)
  • Electron
  • Electron Builder

Step 1: Setting up the Project

The first step is to create a new project and install the required dependencies. We can do this by using the following commands:

mkdir electron-project
cd electron-project
npm init -y
npm install electron electron-builder --save-dev

This will create a new project directory called ‘electron-project’ and install the required dependencies. Now, we need to set up the project’s directory structure by creating the main file and the HTML file for our application.

Step 2: Creating the Application

We will create a basic Electron application to demonstrate the process of creating a Windows installer using Electron Builder. Create a new file called ‘main.js’ in the root directory of the project and add the following code to it:

const { app, BrowserWindow } = require('electron')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

This code sets up the Electron application and creates a new browser window when the application is launched. It also loads the ‘index.html’ file in the window.

Now, let’s create the ‘index.html’ file in the root directory of the project and add the following code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Electron Windows Installer</title>
  </head>
  <body>
    <h1>Electron Windows Installer Tutorial</h1>
    <p>This is a basic Electron application.</p>
  </body>
</html>

This code creates a simple HTML page with a title and a message.

Step 3: Configuring Electron Builder

Now that we have the application set up, we can move on to configuring Electron Builder to create a Windows installer for our application. In the root directory of the project, create a new file called ‘electron-builder.json’ and add the following code:

{
  "appId": "com.example.electron-project",
  "productName": "Electron Windows Installer",
  "directories": {
    "output": "dist"
  },
  "win": {
    "icon": "icon.ico",
    "target": "nsis"
  }
}

This code configures the settings for Electron Builder. The ‘appId’ field is a unique identifier for our application, the ‘productName’ field is the name of our application, the ‘directories’ field specifies the output directory for the installer, and the ‘win’ field specifies the settings for the Windows installer.

The ‘icon’ field specifies the icon file for our application. The ‘target’ field specifies the target installer format, which, in this case, is ‘nsis’, the Windows installer format.

Step 4: Building the Installer

We are now ready to build the Windows installer. Run the following command in the root directory of the project:

npx electron-builder build --windows

This command builds the Windows installer for our application and places it in the ‘dist’ directory.

Step 5: Testing the Installer

Finally, we can test the Windows installer by running the executable file in the ‘dist’ directory. Double-click on the executable file and follow the prompts to install the application.

Congratulations! You have successfully created a Windows installer using Electron Builder.

Customizing the Installer UI and Features


Customizing the Installer UI and Features

One of the main benefits of using Electron for your desktop application is the ability to customize the installer UI. This not only enhances the user experience but also makes your application look more professional. In this section, we will explore the steps involved in customizing the installer UI and features in Electron.

Customizing the installer UI in Electron is achieved using the electron-winstaller package. This package offers a range of customization options to tailor the installer to your needs. One of the simplest ways to customize the installer is by replacing the default icon with your own. The icon can be any PNG or ICO file that you want to use. To replace the default icon, create an icon file with the name “icon.ico” and save it in the root directory of your Electron project. Now, include the following code in the “package.json” file:

“`json
“build”: {
“appId”: “com.example.app”,
“mac”: {
“icon”: “icon.icns”
},
“win”: {
“icon”: “icon.ico”
}
}
“`

In this example, we have specified the path to the icon file in the “win” object under the “build” section.

Another way to customize the installer UI is by adding a license agreement that the user must accept before installing the application. To do this, create a file named “license.txt” and save it in the root directory of your Electron project. Now, modify the “package.json” file as follows:

“`json
“build”: {
“appId”: “com.example.app”,
“mac”: {
“icon”: “icon.icns”
},
“win”: {
“icon”: “icon.ico”,
“legalCopyright”: “Copyright (C) 2022 Example Inc.”,
“setupIcon”: “icon.ico”,
“license”: “license.txt”
}
}
“`

Here, we have added the “legalCopyright”, “setupIcon”, and “license” properties
to the “win” object. Additionally, you can customize the style of the installer by modifying the CSS. You may also include a custom EULA agreement and a custom background image.

Another feature that can be customized in the installer is the ability to install additional dependencies and run scripts before or after the installation process. This can be achieved using the Squirrel.Windows framework, which is used by Electron for updating applications. This framework allows you to execute custom commands before, during, and after installation. The following code shows how to execute a command before install:

“`javascript
const path = require(‘path’);
const squirrel = require(‘electron-squirrel-startup’);

if (squirrel) {
// Before Squirrel can install the app, install dependencies
require(‘child_process’).execSync(‘npm install’, { cwd: path.resolve(__dirname, ‘..’) });
process.exit(0);
}
“`

In the above example, we have included the electron-squirrel-startup package to run before the install process. The package checks if the app is being installed for the first time by Squirrel installer, and if so, installs the required dependencies.

Customizing the installer UI and features in Electron requires some technical knowledge, but the results are well worth the effort. You can tailor the installer to your application’s needs, making it more user-friendly and professional-looking. Remember to keep it simple and consistent with your branding, and most importantly, test it thoroughly before release.

Best Practices for Electron Windows Installer Deployment


Electron Windows Installer Deployment

Electron is a popular framework for building cross-platform desktop applications using web technologies such as HTML, CSS, and JavaScript. The framework allows developers to create native-like apps for Windows, Mac, and Linux operating systems. With its advantages over other platforms, it has been used by many popular companies worldwide. In this article, let us discuss the best practices for Electron Windows Installer Deployment to ensure that your application gets the best possible user experience:

1. Build Your Installer on a Windows Machine


Windows machine

When creating an installer for Windows, it is best practice to do it on a Windows machine. It is because the Windows operating system has specific settings and configurations that may be different from those on macOS or Linux operating systems. By doing this, you ensure that your installer will work seamlessly with Windows-only features such as the registry or system service.

2. Use Code Signing Certificates


Code signing certificate

Code signing certificates help to establish trust between the user and your application. It ensures that your installer is authentic and has not been tampered with. It also prevents any warning messages from the operating system, which may prevent the user from installing the application. Therefore, ensure that you use a code signing certificate from a trusted provider and sign your installer before releasing it.

3. Include Language Packs


Language packs

If your application has a global audience, it is important to include language packs with your installer. This ensures that users who do not speak the default language of your application can still use it in their preferred language. You can include multiple language packs with your installer, and the user can choose their preferred language during the installation process. Including language packs is a good way to improve your application’s user experience and increase its popularity.

4. Bundle All Dependencies


Bundle dependencies

When building your installer, ensure that you bundle all your application dependencies. This ensures that the user gets the same environment that you tested your application in and prevents any compatibility issues from arising. Bundling dependencies also makes it easy to deploy and update your application as the user does not have to worry about installing any additional dependencies.

5. Optimize Your Installer Size


Optmize installer size

Users tend to avoid or uninstall applications with large installer sizes. Therefore, optimizing your installer size is crucial to improve your application’s user experience. One way to do this is by compressing all your application files using a compression algorithm. This reduces the installer size and makes it easier and faster to download. Additionally, ensure that you remove any unnecessary dependencies or files that do not contribute to the application functionality.

Another way to optimize your installer size is by using a differential update mechanism. This updates the user’s application with only the necessary changes instead of downloading the entire installer again. This reduces the bandwidth usage and ensures that the user has the latest version of your application.

In conclusion, following these best practices ensures that your Electron Windows Installer Deployment goes smoothly and that your application provides the best user experience possible. By creating a polished and efficient installer, you improve your application’s chances of success and popularity among users.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top