Create a Desktop App With JavaScript & Electron

Hey there! In this video, you’ll learn how to create a cross-platform desktop app using JavaScript and Electron. Electron is a powerful tool that allows you to build apps for different platforms using HTML, CSS, and JavaScript. It’s used by popular applications like VS Code and Atom. The video will guide you through creating a desktop app specifically for resizing image dimensions. You’ll be able to select an image, change its width and height, and see the result. The video assumes knowledge of JavaScript, but not necessarily Node.js. It covers topics such as the main process and renderer process in Electron, creating windows and menus, error handling, and more. You’ll also find the final code and theme files in the provided repository. So grab a cup of coffee or tea, and let’s dive into the world of JavaScript, Electron, and desktop app development!

In this video by Traversy Media, you’ll learn how to create a cross-platform desktop app called “Image Resizer” using JavaScript and Electron. The app allows you to select an image, change its dimensions, and see the result. Throughout the video, you’ll gain a solid understanding of Electron’s main process and renderer process, as well as how to create menus, customize windows, handle errors, and communicate between the main process and renderer process. The video assumes basic JavaScript knowledge, and the code and theme files for the project can be found in the provided repository. So if you’re ready to explore the world of Electron app development, join Traversy Media and start building your own desktop app!

Understanding Electron

Electron is a powerful tool that allows developers to build cross-platform desktop applications using HTML, CSS, and JavaScript. It is widely used in the industry and has been the foundation for popular applications like VS Code, Atom, and Postman. In this article, we will explore the significance of Electron and how it works, as well as provide examples of popular apps built with Electron.

What is Electron?

Electron is a framework that enables developers to create desktop applications using web technologies. It combines the Chromium rendering engine and Node.js runtime environment to provide a seamless experience across different operating systems. With Electron, developers can leverage their existing web development skills to create native-like applications for platforms like Windows, macOS, and Linux.

Significance of Electron

Electron has gained popularity due to its ability to develop cross-platform applications with ease. Traditionally, building native applications for different platforms required learning different programming languages and frameworks. Electron simplifies this process by allowing developers to write a single codebase using HTML, CSS, and JavaScript, which can then be packaged and deployed for multiple platforms without significant modifications.

This means that developers can build applications for Windows, macOS, and Linux simultaneously, saving time and effort. Moreover, Electron provides access to native system APIs, enabling developers to create apps that have the same capabilities as native applications, including file system access, use of system tray, notifications, and more.

Examples of popular apps built with Electron

Electron has been used to create numerous well-known applications. Let’s take a look at some examples:

  1. Visual Studio Code (VS Code): VS Code is a highly popular source code editor developed by Microsoft. It is built entirely with Electron and offers a wide range of features for developers, including built-in Git integration, a rich extension library, and a customizable user interface.

  2. Atom: Atom is another popular code editor built on Electron. It is known for its extensibility and offers a wide range of community-developed plugins for enhancing the editing experience.

  3. Slack: Slack, the widely-used collaboration platform, is built on Electron. It allows users to communicate and collaborate in real-time, offering features such as channels, direct messaging, and file sharing.

  4. Postman: Postman is a popular API development tool that is also built with Electron. It provides a comprehensive set of features for testing, documenting, and monitoring APIs, making it a valuable tool for developers and teams.

These examples demonstrate the versatility and power of Electron in creating desktop applications that can be used across different platforms.

Pre-requisites

Before diving into building applications with Electron, it is recommended to have basic knowledge of JavaScript. Familiarity with concepts such as variables, functions, and object-oriented programming will be beneficial. Additionally, having some experience with web technologies like HTML and CSS will help in understanding the development process.

While knowledge of Node.js is not required, having a basic understanding of its concepts and how it interacts with JavaScript will be valuable. However, this article will cover the necessary information about Node.js as it relates to Electron.

How Electron Works

To understand how Electron works, it is important to grasp the concept of the main process and the renderer process.

Main Process vs Renderer Process

In Electron, the main process is responsible for managing the lifecycle of the application, interacting with the operating system, and creating and managing windows. It runs in a Node.js environment and has access to both native APIs and the Chromium rendering engine.

On the other hand, the renderer process runs in separate instances for each open window. It is responsible for rendering the UI of the application and executing JavaScript code.

Function of main.js file

The main.js file serves as the entry point for the main process in Electron. It handles tasks like creating windows, handling events, and interacting with the renderer processes. This file is where developers define the behavior of their application, such as creating the main window, loading content, and handling user interactions.

Creating and manipulating windows

Electron allows developers to create and manage windows using the browser window object. Through this object, developers can set properties such as window dimensions, title, and position. Additionally, windows can be customized further by adding features like menus, notifications, and system tray icons.

Building a Desktop App with Electron

Now that we understand the basics of Electron, let’s dive into building a desktop app. In this section, we will walk through the process of building an image resizer app as an example.

Building an image resizer

The image resizer app allows users to select an image and change its width and height. It is a simple yet practical demonstration of Electron’s capabilities. The app will have a user interface that displays the selected image and allows users to input dimensions for resizing.

Setting up project structure

To get started, create a new directory for your project and initialize a new package using npm init. This will create a package.json file which will hold information about your project and its dependencies.

Next, install Electron by running npm install electron. This will install Electron as a dependency for your project.

Starting your first Electron application

To start building the Electron application, create a new file called main.js. This file will serve as the entry point for the main process.

In main.js, import the necessary modules and set up the basic boilerplate code for creating the main window. This includes creating a BrowserWindow instance, setting the window properties such as width and height, and loading the content in the main window.

After setting up the main window, listen for the ready event and create the main window and load the content in it.

Now, when you run your Electron application using electron . or npm start, the main window will open, displaying the content you specified. In our case, the content will be a file called index.html located in the “Renderer” folder.

Packing the Application

Once you have finished building your Electron application, you may want to package it for distribution. Electron provides tools like Electron packager and Electron Forge to simplify the packaging process.

Use of Electron packager and Electron Forge

Electron packager and Electron Forge are two popular tools for packaging Electron applications. They automate the process of bundling your application’s source files into a distributable format for different platforms.

Packaging the app for different platforms

With Electron packager or Electron Forge, you can package your Electron application for different platforms such as Windows, macOS, and Linux. These tools streamline the packaging process by handling platform-specific dependencies, file structures, and compilation processes.

Deployment of Electron apps

Once your Electron application is packaged, you can distribute it to users on different platforms. You can deploy your application through various channels, such as app stores, websites, or package managers. The deployment process may differ depending on your target platform and the distribution channels you choose.

Creating and Managing Windows in Electron

In Electron, creating and managing windows is a crucial aspect of building desktop applications. Let’s explore how to create the main window, set properties for the main window, and load content within the main window.

Creating the main window

In Electron, the BrowserWindow class is used to create and manage windows. To create the main window, you will need to create an instance of BrowserWindow and specify the desired properties for the window.

The main window is typically created in the main process and is responsible for the overall user interface of the application.

Setting main window properties

The main window’s properties, such as width, height, title, and more, can be customized to fit your application’s requirements. You can set these properties using methods available in the BrowserWindow class.

By setting properties like width and height, you can define the initial dimensions of the main window. Similarly, you can set the title of the main window to make it meaningful and recognizable to users.

Loading content in the main window

To display content within the main window, you need to specify the content’s source. In Electron, this is typically achieved by loading an HTML file into the main window.

You can use the loadFile() method of the BrowserWindow object to load an HTML file into the window. This method takes in the path to the HTML file as an argument and renders the contents within the main window.

Automating Tasks

When developing an Electron application, automating tasks like app restarts can greatly improve development efficiency. There are tools like Node mon and Electron mon that can help with automating app restarts.

Node mon and electron mon tools

Node mon and Electron mon are command-line tools that monitor changes in your application’s files and automatically restart the application whenever a change is detected. This eliminates the need for manual restarts during development, making the development process smoother and more efficient.

By using these tools, you can focus on making changes to your application without the hassle of constantly restarting it to see the updates.

Customizing the Application

Customizing the appearance and behavior of your Electron application is an essential part of creating a seamless user experience. There are various ways to customize an Electron application, including utilizing theme files and assets, customizing the Electron menu, and creating an about window.

Utilizing theme files and assets

To give your Electron application a consistent theme, you can utilize theme files and assets. These files contain CSS styles, images, and other resources that define the visual appearance of your application.

By incorporating theme files and assets into your Electron application, you can create a visually appealing and cohesive user interface.

Customizing Electron menu

The Electron menu is an important component of the application’s user interface. It provides users with access to various functionalities and options.

Electron allows you to customize the menu by creating a menu template. The menu template consists of menu items that define the structure and functionality of the menu.

By using menu roles, you can specify menu items for common actions such as file operations, editing, and more. This allows users to interact with the application using traditional menu-based navigation.

Creating an about window

An about window is a common feature in many desktop applications. It provides users with information about the application, such as the version number, developer details, and additional resources.

In Electron, you can easily create an about window by adding a menu item to the application menu. When clicked, this menu item opens a new window that displays the desired information.

Creating an about window adds a professional touch to your Electron application and enhances the user experience by providing essential information to users.

Handling Errors and User Feedback

Error handling and user feedback are crucial aspects of any application. Electron provides multiple ways to handle errors and communicate with users effectively.

Implementation of Toastify.js

To display user-friendly alerts and toasts in your Electron application, you can make use of libraries like Toastify.js. Toastify.js allows you to show visually appealing notifications, success messages, and error messages, improving the overall user experience.

By implementing Toastify.js, you can enhance your application’s error handling capabilities and provide users with meaningful feedback.

Creating alerts

In addition to using libraries like Toastify.js, Electron provides native dialog APIs for creating alerts. These APIs allow you to display modal dialogs, notifications, and other types of alerts to communicate important information to users.

You can choose from a variety of native alerts provided by Electron, such as error dialogs, warning dialogs, and informational dialogs. These alerts can be customized with your own messages and buttons to meet the specific requirements of your application.

Inter-process communication (IPC)

Inter-process communication (IPC) is a powerful feature of Electron that allows communication between the main process and renderer processes. This enables you to send and receive data between different parts of your application.

With IPC, you can handle events, share data, and synchronize actions between the main process and renderer processes. This is especially useful when you need to update the UI based on changes in the main process, or when you want to send data from the renderer process to the main process for further processing.

Implementing IPC in your Electron application allows for seamless communication between different components, enhancing the overall functionality and performance.

Conclusion

Electron offers developers a powerful and flexible framework for building cross-platform desktop applications using web technologies like HTML, CSS, and JavaScript. Its significance lies in its ability to streamline the development process and enable the creation of native-like applications for multiple platforms.

In this article, we explored the basics of Electron, including its architecture, the main process, and the renderer process. We also learned how to create and manage windows, automate tasks, customize the application, handle errors and user feedback, and deploy Electron apps.

Further exploration of Electron can lead to many possibilities, including integrating additional functionalities, expanding on the user interface, or exploring more advanced features like native module integration. The provided repository contains additional information and resources for your journey of building Electron applications.

Electron has revolutionized the way developers create cross-platform desktop apps, making it accessible and efficient. With its growing popularity and robust capabilities, Electron continues to be a favored framework for building powerful and versatile applications.

Watch The Tutorial Now