Preventing multiple same ElectronJS apps to run at single time



I am building an EelectronJS app and successfully release it as an executable Windows program.

But the problem is when the app is already running, if I double-click again that .exe file, the second window of this app is running, but buggy. I don’t want it. So here is the solution:

This main.js file is my solution when I need to prevent multiple instances of ElectronJS app to run at same time.

const { app, BrowserWindow } = require('electron')

const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
	app.quit()
} else {
	app.on('ready', createWindow)
}

function createWindow () {
	// Create the browser window.
	let win = new BrowserWindow({ width: 1024, height: 720 })
	
	// and load the index.html of the app.
	win.loadFile('index.html')
	
}

app.on('window-all-closed', () => {
  app.quit()
})

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *