ElectronJS Build
Phan Quang Đại
Mar 25, 2020
Structure
root folder
|---main.js
|---package.json
|---index.html
Instruction
- First of all, you need to install the Electron
npm install -g electron
- Let’s go to the folder where you will contains your source
npm init -y
- Then it will create basic files.
main.js package.json index.html
Remember to edit the dependencies, correct it with the right directory!
- The package.json has the format like this!
{ "name": "electron-aloha", "version": "1.0.0", "description": "Aloha", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "electron-squirrel-startup": "^1.0.0" } }
- I will go into the main.js and and some code here!
const {app, BrowserWindow, Menu} = require('electron'); const path = require("path"); const url = require("url"); let win; function createWindow(){ win = new BrowserWindow(); Menu.setApplicationMenu(null); //win.webContents.on("devtools-opened", () => { win.webContents.closeDevTools(); }); win.loadURL(url.format({ pathname : path.join(__dirname, 'index.html'), protocol: 'file', slashes: true })); win.on('closed', ()=>{ win = null; }) } app.on('ready',createWindow); app.on('window-all-closed', ()=>{ if (process.platform!=='darwin'){ app.quit() } }); app.on('active', ()=>{ if(win===null){ createWindow() } });
-
And then, I will add some appearance into index.html
- Okay, all is set up! Now let’s run it!
electron .