Making your first Desktop Application with Electron
Use Javascript to build desktop apps 🎉

I am a developer from Delhi, India
Search for a command to run...
Use Javascript to build desktop apps 🎉

I am a developer from Delhi, India
free to write an issue or start a discussion at
DISCLAIMER: This might not be the most well-written article since I haven't written in a while and I just wanted to get back to writing with a short article about my experience of the day, so sorry about that I previously used this repository for my...

The release of NextJS 13 brought about a plethora of new and impressive features, with one standout being the updated data fetching and management process. The fetch API replaced the more complicated functions, including getServerSideProps, getStatic...

Looking for a safe and reliable way to authenticate users? Consider implementing magic links. They offer a secure alternative to traditional passwords and can help mitigate the risk of password leaks and forgotten passwords. Magic Link authentication...

Neovim is a powerful text editor that can be customized with plugins to enhance its functionality. In this article, we will explore the top 10 essential Neovim plugins. These plugins can help improve your Neovim experience by adding features such as ...

A type-safe and zero-runtime version of Tailwind CSS

If you're a web developer who uses a lot of Javascript, and want to make a desktop application without having to learn anything else, Electron is the right thing for you!
With Electron, you can build cross-platform applications with Javascript, HTML, and CSS.
Let's start of by creating a project and installing electron in it as a NPM Dev dependency
mkdir my-electron-app && cd my-electron-app
npm init -y
npm install -D electron
Your project structure should be like below
my-electron-app/
|--node_modules/
|--package.json
|--main.js
|--index.html
The main.js file will serve as the entry point for our electron application. It will run the main process to serve our application, control the lifecycle of the application, display the GUI, perform native OS interactions, create Renderer processes, etc.
const { app, BrowserWindow } = require('electron');
We need these two modules to
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webpreferences: {
nodeIntegration: true
}
})
win.loadFile('index.html')
}
app.whenReady().then(createWindow)
In this function, we are creating a Browser Window. We have set the width and the height to 800 and 600, respectively and have enable node integration. Then we are loading the index.html file in our window and serving it
You can also serve a url instead of a file by using
win.loadURL('http://localhost:3000')
// OR
win.loadURL('https://livecode247.com')
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
In this function, we quit the application when all the windows have been closed. However, we do not do it in a Darwin(OSX) because of the different window management process in the same
You add a new listener that creates a new browser window only if when the application has no visible windows after being activated. For example, after launching the application for the first time, or re-launching the already running application.
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
Now that we have finished with the main script file, let's go to our HTML part
Add the following to the index.html file created earlier.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>Hello World!</h1>
</body>
</html>
This is just a normal html file which has a heading Hello World!
Now, before we serve the application, we need to make some changes to the package.json file.
Update the "main" key to look like this
"main": "main.js",
Let's add a start script to our application like so,
"scripts": {
"start": "electron ."
},
That's it! Now, let's try it out.
Run the following in the terminal
npm start
Now, an application window should startup in your device and should show this

You can also add push notifications with electron. Let's try it out
Add the following to your index.html
<script>
const myNotification = new Notification('Title', {
body: 'Notification from Rederer process'
})
myNotification.onclick = () => {
console.log('Notification clicked')
}
</script>
Now, when the app starts up, you should see, something like this

Note: This will differ from OS to OS. The above is an image taken on OSX.
And when you click it, you should see Notification Clicked in the console.
You can open the developer toold by running Cmd-option-i on a Mac or Ctrl-alt-i on Windows/Linux.
That's it for this