Open In App

Introduction to ElectronJS

If you are interested in using Web Development skills for developing apps for different desktop platforms like Windows, Linux or macOS, Electron is the perfect framework that suits your needs. Let’s start with a brief introduction to Electron. 

ElectronJS: Electron was developed lately in 2013 by the open-source and version control giant, GitHub. Electron uses NodeJs in its core to serve pages built on HTML and CSS as a desktop app. This implies that developers comfortable in HTML5 or Android Development can easily switch their platform to Electron. 



The Electron app may be categorized into two main processes, namely, Main and Renderer process. 

Performing Culture: There are two process in the Electron performing Culture:



Electron supports various APIs for both the main and renderer processes, which helps to interact with the desktop operating system and its resources. 

Prerequisites: The main prerequisites which may be considered important for starting on with Electron are listed below.

Installing Electron: Let’s start with the building blocks of development with Electron.

node -v
npm -v

npm install electron --save-dev 




{
  "name": "electronapp",
  "version": "1.0.0",
  "description": "Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [
    "Electron"
  ],
  "author": "xyz",
  "devDependencies": {
    "electron": "^7.1.7"
  }
}




const { app, BrowserWindow } = require('electron')
 
let win
 
function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    icon: ( './icon.png'),
    webPreferences: {
      nodeIntegration: true
    }
  })
 
  // and load the index.html of the app.
  win.loadFile('./index.html')
  //win.loadURL('https://google.com/')
 
  // Open the DevTools.
   win.webContents.openDevTools()
 
  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}
 
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
 
// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
 
app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})




<!DOCTYPE HTML>
<html>
    <head>
    <title>my first electron app</title>
    </head>
 
    <body>
        <h1>my first electron app</h1>
    </body>   
</html>




npm install --unsafe-perm=true

will download necessary node_modules, required by electron to render a page. 

 

Output:

 

References: https://electronjs.org/docs/tutorial/first-app#installing-electron


Article Tags :