初めてのElectronアプリケーション
ステップバイステップで基本的な「Hello World」Electronアプリケーションを作成し、mainプロセスとrendererプロセスの基礎を学びます。
「初めてのElectronアプリケーション」はCoddyKit上の無料Electron Desktop App Developmentレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはElectron Desktop App Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Hello, Electron World!
Time to build your first Electron app — a simple desktop "Hello World." It introduces the core files that bring web tech to the desktop.
Setting Up Your Project
First, scaffold the project: make a folder, run npm init, then install Electron as a dev dependency. The commands are below.
The Main Process (main.js)
Every app has a main process — a Node.js environment that controls the lifecycle, talks to the OS, and creates windows. It starts in main.js.
main.js: Basic Window Setup
In main.js, you import Electron modules and define how to create a window that will host your web content. Try the basic setup below.
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
// We'll load our HTML file here soon!
}
app.whenReady().then(createWindow);
// Handle closing all windows for non-macOS
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Handle re-activating app on macOS
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});The Renderer Process (index.html)
The renderer process is where your UI lives — essentially a Chromium window rendering HTML, CSS, and JS. It loads from index.html.
index.html: Our UI Content
This index.html holds your Hello World message — just an ordinary web page, exactly like any you'd build for the browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Electron App</title>
<style>
body { font-family: sans-serif; text-align: center; padding: 50px; background-color: #f0f0f0; }
h1 { color: #333; }
p { color: #666; }
</style>
</head>
<body>
<h1>Hello from Electron!</h1>
<p>This is your very first desktop application.</p>
</body>
</html>Connecting Main & Renderer
Now connect the two: tell the window to display your page with win.loadFile() inside createWindow. One line wires UI to the main process.
main.js: Loading HTML
Here is main.js updated to load the page. Note how __dirname locates index.html relative to the script, no matter where it runs.
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
win.loadFile(path.join(__dirname, 'index.html')); // Load the HTML file
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});Running Your Electron App
Add a start script to package.json so you can launch the app with one command instead of typing electron each time.
package.json: Start Script
Your package.json should look like this. "electron ." runs main.js in the current folder — save it, then run "npm start" to launch.
{
"name": "my-electron-app",
"version": "1.0.0",
"description": "My first Electron app",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^28.0.0"
}
}Quick Check: Main Process Role
You've seen how main.js and index.html work together. Let's test your understanding of their primary roles.
Recap: Your First Electron App
You built your first Electron app: a main process for lifecycle and windows, a renderer for UI, wired together with a start script.
よくある質問
「初めてのElectronアプリケーション」レッスンは無料ですか?
はい。「初めてのElectronアプリケーション」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Electron Desktop App Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
「初めてのElectronアプリケーション」で何を学びますか?
ステップバイステップで基本的な「Hello World」Electronアプリケーションを作成し、mainプロセスとrendererプロセスの基礎を学びます。 ブラウザで直接実行するハンズオンコードでElectron Desktop App Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Electron Desktop App Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのElectron Desktop App Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「初めてのElectronアプリケーション」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このElectron Desktop App Developmentレッスンでコードを書いて実行できますか?
はい。すべてのElectron Desktop App Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Electronとは
- 開発環境のセットアップ
- 初めてのElectronアプリケーション
- Electronのアーキテクチャを理解する