Thought i’d make a simple Electron hello world tutorial application. Electron is a framework for building desktop apps with CSS, Javascript and HTML. The apps will be cross platform and work on Windows, MacOS and Linux distributions.
You can download this application from Github if you’d like. After this blog post was written i decided to rebrand it to the electron tutorial app because that name suits it better when adding more electron tutorials to it.
An electron app is built using the node.js framework and is brought to us by Github. Maybe you’ve already tested a few electron apps now. For example slack is a well know application built on electron. If you’d like to test one i can recommend Atom. In fact, why don’t you try this tutorial with that IDE?
Video introduction
1. Installing Node.js
First we need to install node.js. So go ahead and do that.
2. Installing electron prebuilt
Now that we have Node.js installed, let’s install electron prebuilt globally.
sudo npm install -g electron
This makes us able to test run our electron application by running this command in our terminal. But more on this later.
electron .
2. File structure
In its most basic form an electron app needs three files. package.json, main.js, and index.html. Making the folder structure look like this:
electron-hello-world/
- index.html
- main.js
- package.json
Create a folder and add those files and let’s walk trough them one by one.
3. package.json
Let’s add content to package.json. We need a name, a version and a main setting that points to a script that will handle the starting up of the app. If main is not present electron will look for a file called index.js.
{
"name" : "electron-hello-world",
"version" : "0.1.0",
"main" : "main.js"
}
4. Main.js
As mentioned above the we pointed out main.js to handle the startup of our application. Lets add this content to it. This code is borrowed from electron quick start.
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)
// Open the DevTools.
mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
5. index.html
The index.html takes care of the gui.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Electron Hello World!</title>
</head>
<body>
<h1>Electron Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>
6. Running the Electron hello world app
Now let’s get back to the electron . command. That will start electron and run the app that we are currently building.
electron .
This fires up an app looking like this:
As you can see we also have the chromium developer tool window opened inside our app. This is opened with this call in main.js.
mainWindow.webContents.openDevTools()
7. Read more
Next tutorial covers testing your electron app in Ubuntu. If you are not interested in testing it in Ubuntu the next tutorial after that is Electron app navigation.
Read more about electron at the official site, and check out the API.