0

I am building an electron app with the use of eel as well, although I am not sure that is relevant here. I recently came back to this project, and I'm sure it was working before. Everything works except for the minimise and close buttons which are custom ones.

Here is the HTML

<div id="title-bar">
    <div id="title-bar-btns">
        <button id="min-btn" onclick="window_minimise()">&#x2014;</button>
        <button id="close-btn" onclick="window_close()">&#x2715;</button>
    </div>
</div>

And the JS

function window_minimise(){
    const remote = require('electron').remote;
    var window = remote.getCurrentWindow();
    window.minimize();
}

function window_close(){
    const remote = require('electron').remote;
    eel.quit_app();
    var window = remote.getCurrentWindow();
    window.close();
}

And the Python code

@eel.expose
def quit_app():
    sys.exit(0)

I tried running it in my browser via http://localhost:8000/html/index.html and when I click minimise I get this error

Uncaught ReferenceError: require is not defined at window_minimise (index.js:111) at HTMLButtonElement.onclick (index.html:18)

And close

Uncaught ReferenceError: require is not defined at window_close (index.js:117) at HTMLButtonElement.onclick (index.html:19)

Does anyone know what might be going wrong here and how I can fix it?

Thanks.

EDIT:

Here is my electron main.js file

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 1200, height: 748, icon:'web/images/favicon-32x32.png', resizable: false,
  frame: false, show: false, backgroundColor: '#171717'}) // Needs to be changed based on theme
  mainWindow.once('ready-to-show', () => {
  mainWindow.show()
})
  mainWindow.setMenu(null);
  // and load the index.html of the app.
  mainWindow.loadURL('http://localhost:8000/html/index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.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.
    mainWindow = 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', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active unti, l the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X 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 (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

There are some errors in it (missing semicolons and let mainWindow), but those errors are also in the electron-quick-start main.js so I'm quite confused.

J P
  • 541
  • 1
  • 10
  • 20

2 Answers2

1

As I explained in: https://stackoverflow.com/a/56289565/1907797

You have to set nodeIntegration to true in your BrowserWindow webPreferences since the version 5.0.0 the default values of nodeIntegration and webviewTag are false to improve security. Electron associated PR: 16235

const mainWindow = new electron.BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
});
Damien
  • 3,915
  • 1
  • 17
  • 18
  • Do you mean in my main.js file? So `mainWindow = new BrowserWindow({width: 1200, height: 748, icon:'web/images/favicon-32x32.png', resizable: false, frame: false, show: false, backgroundColor: '#171717', webPreferences: {nodeIntegration: true}})` – J P May 24 '19 at 09:28
  • Yep, it should fix it, do you still have the exact same error ? – Damien May 24 '19 at 09:40
  • Did you restart your electron process ? And in your devtools console if you type `require` it retrurns `undefined` ? – Damien May 24 '19 at 10:00
  • I restarted my python file which should execute the electron process. It says `VM55:1 Uncaught ReferenceError: require is not defined` – J P May 24 '19 at 10:05
  • Oh yes you are using eel, so it's not exactly electron. I think you don't need to use require and remote but simply expose a python function that call mainWindow.close from your python code. – Damien May 24 '19 at 10:14
  • I had this working before so I think the issue is due to a change in Electron (I last worked on it about 9 months ago). Perhaps something in my main.js file is different to the new version or something similar. – J P May 24 '19 at 10:15
  • Yes as explained in my answer electron made few changes about it in electron 5.0.0 (April 24, 2019), that's why I told you to add nodeIntegration in the webPreferences. Maybe you can check on the eel side, or try to downgrade to an older electron version. – Damien May 24 '19 at 10:19
  • The thing is, I'm only on version 2.0.0. I don't think eel has any involvement and I wouldn't be sure how to add `mainwindow.close` to it anyway. – J P May 24 '19 at 15:24
0

Please enable nodeIntegration in index.html, then only you can use require in renderer page

Sharvin K
  • 697
  • 3
  • 10