Electron is a framework that allows you to build cross-platform desktop applications using HTML, CSS, and JavaScript. It uses Node.js for backend logic and Chromium for rendering the frontend. With Electron, you can easily convert your main.html into a macOS .app file that runs independently of the browser.
Here’s a step-by-step guide to setting up and using Electron to convert your HTML file into a macOS app.
1. Prerequisites:
- Node.js: Electron is built on top of Node.js, so you need to have it installed. You can download and install it from Node.js official site.
- Electron: This will be installed later as part of the setup.
2. Set up your project directory:
Create a new folder for your Electron app. In this example, we’ll call it electron-app and place your main.html in this directory.
bash
mkdir electron-app
cd electron-app
3. Initialize the project:
Inside your project folder, run npm init to create a package.json file. This file will hold the metadata and dependencies of your app.
The -y flag accepts the default options for the package.json file. You can modify these later if needed.
4. Install Electron:
Install Electron as a development dependency by running:
bash
npm install electron --save-dev
This will download Electron and its dependencies into your project.
5. Create the Electron main script:
You need a JavaScript file to control the Electron app, called main.js. Create a new file named main.js in the root of your project folder.
main.js:
javascript
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
},
});
win.loadFile('main.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
This script creates a window (using Chromium) and loads your main.html file into that window when the app starts. It also includes basic app lifecycle handling for macOS (where apps usually stay open until explicitly quit).
6. Create the HTML file:
Ensure your main.html file is in the root folder of your project (electron-app). Here's a basic example:
main.html:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Electron App</title>
</head>
<body>
<h1>Hello, Electron!</h1>
<p>This is my standalone app running on Electron!</p>
</body>
</html>
7. Run your Electron app:
To launch your app, you can add a start script to your package.json. Open package.json and modify the "scripts" section like this:
package.json:
json
{
"name": "electron-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^26.0.0"
}
}
Now, in your terminal, run the following command to launch your Electron app:
This will open a window displaying your main.html file in a standalone Electron app!
8. Package the app for macOS:
To package your Electron app into a .app file for macOS, you can use a tool called electron-packager.
Install it globally or as a development dependency:
bash
npm install electron-packager --save-dev
Now, package the app by running the following command:
bash
npx electron-packager . MyElectronApp --platform=darwin --arch=x64 --icon=path/to/icon.icns
MyElectronApp is the name of the app.--platform=darwin specifies macOS as the target platform.--arch=x64 is the architecture for macOS (you can also specify arm64 for Apple Silicon Macs).--icon is optional if you want to add a custom app icon.
After running this command, a MyElectronApp-darwin-x64 (or MyElectronApp-darwin-arm64 for M1 Macs) folder will be created containing the .app file. You can move this .app file to any Mac and run it as a standalone application!
9. Distribute the app:
- Once packaged, you can distribute the
.app file to macOS users by simply sharing the .app file or by using a tool like electron-builder to create a .dmg installer.
To use electron-builder:
bash
npm install electron-builder --save-dev
Add a build script to your package.json:
json
"scripts": {
"build": "electron-builder"
}
Then run:
This will generate a .dmg file (macOS installer) for your app.
Recap:
- Install Electron and set up the project structure.
- Create a
main.js script to load your HTML file into a window. - Test the app by running
npm start. - Package the app using
electron-packager for distribution as a macOS .app.
Electron is highly customizable, so if you need more advanced features like interacting with the file system or using native APIs, you can integrate those into your app. Let me know if you'd like help with any specific aspect!