How to build Electron apps with React.

Introduction

I have always had a deep interest in writing Desktop applications. What was holding me off throughout the years was, I wasn't interested in learning C++ or Java to make them. I wanted to build Desktop apps with Javascript, so I landed on Electron, a Javascript Desktop application framework, developed and maintained by Github.

Electron is such a big deal that it will allow you to create Desktop apps for all the major OS with a single codebase. That impressed me but not so much as you can write Desktop applications with React, Angular, Vue or any javascript framework out there. They sold me on that.

In this tutorial, I will outline the initial setup to get you started.

Prerequisites

To understand the syntax and concept of this tutorial you should have:

Basic concepts of Javascript and React

Written Javascript in ES5 and ES6 standard

Used Node Package Manager(npm)

Installed Nodejs and npm/yarn

Get started

Let's create the folder to house the electron app and change the directory to it.

$ mkdir electron_react_app && cd$_

Use CRA(create-react-app) cli to generate a react codebase for us

$ npx create-react-app .

Since we working with electron then we will add relevant dependencies throughout the tutorial.

$ yarn add electron electron-builder wait-on concurrently -D

electron - allows us to use the electron framework.

electron-builder - allows us to build the electron app to executable.

wait-on - lets u wait on react to compile during development so as to render it with electron.

concurrently - allows us to run both react and electron concurrently.

$ yarn add electron-is-dev

electron-is-dev - tells electron the current environment we are working to decide either serve the build or render the react app running on dev environment.

Create an electron.js file in the public folder to house our electron logic.

The script will wait until CRA compiles the react app then starts the electron app.

Also for you to start the electron app you will have to point where the electron logic code lies. We will do so inside the package.json by adding the main value.

"main": "public/electron.js"

Let's run our app.

$ yarn electron-dev

You should see this.

For a person who has used react before, you should be wondering why a new tab on your browser wasn't opened, this is because of the environment we defined while running electron-dev, BROWSER=none.

Now, if you need to access the fs module as I did, you'll quickly hit the Module not found error. More info here.

Fortunately, it can be solved with the use of electron-renderer as the Webpack target. You will not have to change anything else in your React code rather we will use a third-party library called Rescripts

Note: You will not write any typescript, and I won't pressure you to use it.

We need to define the homepage route because when react builds it uses absolute paths, and electron doesn't do absolute path. You can change this inside the package.json file by adding a homepage property.

You will also want to create a directory called assets where you will add your app icons. Check here to see the formats for these icons.

Now you can package your app

$ yarn electron-pack

Background context

If you happen to encounter an error like this on linux.

[2720:0425/142001.775056:FATAL:setuid_sandbox_host.cc(157)] The SUID sandbox helper binary was found, but is not configured correctly. Rather than run without sandboxing I'm aborting now. You need to make sure that /home/<name>/src/ssbc/patchwork/node_modules/electron/dist/chrome-sandbox is owned by root and has mode 4755.

You can fix that like that by adding with this command.

Ensure you are in the project directory

$ sudo chown root node_modules/electron/dist/chrome-sandbox

Then

$ sudo chmod 4755 node_modules/electron/dist/chrome-sandbox

Your Linux package app won't work automatically because it has the .Appimage extension by default. You will have to make it executable.