ElectronJS Build

Structure

root folder
|---main.js
|---package.json
|---index.html

Instruction

  1. First of all, you need to install the Electron
    npm install -g electron
    
  2. Let’s go to the folder where you will contains your source
    npm init -y
    
  3. Then it will create basic files.
     main.js
     package.json
     index.html
    

    Remember to edit the dependencies, correct it with the right directory!

  4. 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"
      }
    }
    
  5. 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()
     }
    });
    
  6. And then, I will add some appearance into index.html

  7. Okay, all is set up! Now let’s run it!
    electron .