28

I am running an express app via electron.

Below is the main.js

   const electron = require("electron"),
          app = electron.app,
          BrowserWindow = electron.BrowserWindow;

    let mainWindow;

    function createWindow () {
      mainWindow = new BrowserWindow({
          width: 1200,
        height: 800,
        frame: false,
        kiosk: true
      });
      mainWindow.loadURL(`file://${__dirname}/index.html`)
     mainWindow.webContents.openDevTools();

      mainWindow.on("closed", function () {
        mainWindow = null;
      })
    }

    app.on("ready", createWindow);
    app.on("browser-window-created",function(e,window) {
      window.setMenu(null);
    });

    app.on("window-all-closed", function () {
      if (process.platform !== "darwin") {
        app.quit();
      }
    });



    app.on("activate", function () {
      if (mainWindow === null) {
        createWindow();
      }
    });

and below is a button in the view that upon click i would like it to close the app

<button id="close-btn"><i class="fa fa-cube" aria-hidden="true"></i>&nbsp; Close application</button>

Essentially I want to activate this part of the code once the button has been clicked

  app.on("window-all-closed", function () {
          if (process.platform !== "darwin") {
            app.quit();
          }
        });
code_legend
  • 3,547
  • 15
  • 51
  • 95

5 Answers5

43

you can use

const remote = require('electron').remote
let w = remote.getCurrentWindow()
w.close()

to close your app.

if you are using jQuery

const remote = require('electron').remote
$('#close-btn').on('click', e => {
    remote.getCurrentWindow().close()
})

if you are using Vue.js

<template>
    <button @click="close"><i class="fa fa-cube" aria-hidden="true"></i>&nbsp; Close application</button>
</template>

<script>
    const remote = require('electron').remote

    export default{
        data(){
            return {
                w: remote.getCurrentWindow(),
            }
        },
        methods: {
            close() {
                this.w.close()
            }
        }
    }
</script>
Frank He
  • 637
  • 4
  • 9
  • I am using handle bars but will try using jquery – code_legend Apr 10 '17 at 02:48
  • 11
    This is wrong. This just closes the browser window. On a Mac, the app will continue to run. On Windows it closes but only if all windows have been closed. You need to use app.close() – Johann Sep 10 '18 at 15:15
  • 1
    @AndroidDev Your answer is also wrong: it does not work if there is no app yet. I use process.exit() when my Electron app is both a GUI and CLI, and it doesn't create an app unless the GUI is indicated. You still need to kill the Electron process itself. – PeterT Apr 08 '20 at 17:31
  • 1
    This requires the option enableRemoteModule: true in the BrowserWindow(): new BrowserWindow({ webPreferences: { enableRemoteModule: true, } } – Terje Nesthus Feb 04 '22 at 20:04
31

I use the line below to close the app.

window.close()

Alocus
  • 1,860
  • 3
  • 21
  • 32
27

You can also use app.exit:

app.exit(0)

This seems to bypass any event listeners and force the whole app to exit immediately. (Be sure that is what you want though!)

Kip
  • 107,154
  • 87
  • 232
  • 265
1

It is wrong way, If you run window.close(), it just close window that app still run background.

const remote = require('electron').remote
let w = remote.getCurrentWindow()
w.close()

You should close app like this:

const remote = require('electron').remote
let app = remote.app
app.quit()
Ramazan
  • 11
  • 1
-1

in main.js

function createWindow(){
win =new BrowserWindow({
    icon:'./img/code_file.ico',
    frame:false
});
win.maximize();

win.loadURL(url.format({
    pathname:path.join(__dirname,'./login.html'),
    protocol:'file:',
    slashes:true
}));
  // Emitted when the window is closed.
  win.on('closed', function () {
    // 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
  });
}
app.on('ready',createWindow);
app.on('Window-all-closed', () => {
  if(process.platfrom !== 'darwin') {
    app.quit();
  }
});

when all window is closed app.quit() function closed the application;

Vega TV
  • 217
  • 2
  • 10
Abhi
  • 1
  • 1
  • 1
    Read full question then give suggestion. You may suggest `app.exit(0)` use in exist click event. – Rajib Chy Jul 17 '19 at 19:16
  • 3
    `drawin`? Did you mean [`darwin`](https://nodejs.org/api/process.html#process_process_platform)? – Pang Aug 07 '20 at 02:44