Build Cross‑Platform Desktop Apps with Electron: A Quick Hello World Guide

This article introduces Electron, the open‑source framework that lets you create cross‑platform desktop applications using JavaScript, HTML, and CSS, showcases popular apps built with it, and provides a step‑by‑step hello‑world example with code and setup instructions.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Build Cross‑Platform Desktop Apps with Electron: A Quick Hello World Guide

What is Electron?

Electron is an open‑source framework maintained by GitHub that enables developers to build cross‑platform desktop applications using JavaScript, HTML, and CSS. It runs on Windows, Linux, and macOS and is based on Chromium.

Origins and Adoption

Originally created for the Atom text editor, Electron was later open‑sourced. Despite being a young project (currently version 1.1), it powers many popular applications such as:

Atom – GitHub’s text editor

Slack – the desktop client of the unicorn messaging platform

Visual Studio Code – Microsoft’s code editor

Brave Browser – a privacy‑focused web browser

Hello World Example

The following example demonstrates a minimal Electron app.

Project Structure

electron_test
├── package.json
├── main.js
└── index.html

package.json

{
  "name": "deskjs",
  "version": "0.1.0",
  "main": "main.js"
}

index.html

<!DOCTYPE html>
<html>
<head>
  <title>Hello World!</title>
  <meta charset="UTF-8">
</head>
<body>
  <h1>Hello World</h1>
  <h1>第一个 JS 桌面应用</h1>
</body>
</html>

main.js

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
let mainWindow = null;

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

app.on('ready', function() {
  mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadURL('file://' + __dirname + '/index.html');
  mainWindow.openDevTools();
  mainWindow.on('closed', function() {
    mainWindow = null;
  });
});

Running the Application

Ensure Node.js is installed, then install Electron globally:

npm install -g electron-prebuilt

Execute the app by running electron in the project folder or by dragging the electron_test directory onto the Electron console.

Project Repository

Official source code: https://github.com/electron/electron

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cross-platformJavaScriptElectronDesktop DevelopmentTutorial
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.