clemens
clemens

Reputation: 83

ElectronJS: Uncaught TypeError: Cannot read property "BrowserWindow" / "getCurrentWindow" of undefined

First of all, I know that this is a question only a beginner would ask, but after going through more than 50 different solutions, uninstalling npm and installing yarn I have to ask this incredible dumb question. Why doesnt this work? I want to implement a simple titlebar using ElectronJS, the problem that I have is that the buttons (Close / Minimize / Maximize) do not work. The errors that I receive are the following:

The minimize error: titlebar.js:16 Uncaught TypeError: Cannot read property 'BrowserWindow' of undefined at HTMLButtonElement.maximizeApp (titlebar.js:16)

The maximize error: titlebar.js:16 Uncaught TypeError: Cannot read property 'BrowserWindow' of undefined at HTMLButtonElement.maximizeApp (titlebar.js:16)

The quit error: titlebar.js:21 Uncaught TypeError: Cannot read property 'getCurrentWindow' of undefined at HTMLButtonElement.quitApp (titlebar.js:21)

The JavaScript file that I use to control this is called titlebar.js. This is it:

const remote_v = require("electron").remote;

var minimize_v = document.getElementById("minimize");
var maximize_v = document.getElementById("maximize");
var quit_v = document.getElementById("quit");

minimize_v.addEventListener("click",minimizeApp);
maximize_v.addEventListener("click",maximizeApp);
quit_v.addEventListener("click",quitApp);

function minimizeApp(){
  remote_v.BrowserWindow.getFocusedWindow().minimize();
}

function maximizeApp(){
  remote_v.BrowserWindow.getFocusedWindow().maximize();
}

function quitApp(){
  remote_v.getCurrentWindow().close();
}

Since many of the fixes for other problems like this is in the render process, this is the HTML File:

<!DOCTYPE html>
    <head>
        <title>Visionizer</title>

        <link rel="stylesheet" href="css/editor.css">
        <link rel="stylesheet" href="css/titlebar.css" >
    </head>
    <body>
        <div class="container">         
            <div class="titlebar titlebarStyle">  
                <div class="windowTitle"> Visionizer </div>
                <div class="windowControls windowControlsStyle">
                    <button id="minimize">-</button>
                    <button id="maximize">[]</button>
                    <button id="quit">x</button>
                </div>
            </div>
            <div class="editorScreen">
            </div>
        </div>
        <script src="js/titlebar.js"></script>
    </body>
</html>

The weird thing about this is that, after much trying, I decided to copy the code from the tutorial from github, I thought that there may have been an error in my code that I was too dumb to see. It still didn't run. I uninstalled the package with npm and installed it with yarn using yarn global add electron@latest since some people suggested this.

I do not know whether this is important at all, but I will also copy my code from the main.js-file below since I want to be sure that I included everything:

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

function createWindow () {
  const win = new BrowserWindow({
    width: 900,
    height: 800,
    minHeight: 650,
    minWidth: 600,
    frame: false,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('editor.html')
  win.webContents.openDevTools()
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})

And here is the package.json file:

{
  "dependencies": {
    "electron": "^11.0.2"
  },
  "name": "*******",
  "version": "1.0.0",
  "description": "**********",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },  
  "author": "************",
  "license": "MIT"
}

Some questions on the internet were answered by saying that the project was started wrongly, I followed their advice, I start my projects using the yarn start command

Thank you for reading through this.

Upvotes: 0

Views: 8784

Answers (1)

pushkin
pushkin

Reputation: 10199

It looks like your remote module is undefined.

You'd want to set enableRemoteModule: true in your main window's webPreferences, or better yet, scrap remote altogether and do these operations from the main process.

The remote module was disabled in Electron 10.

Upvotes: 2

Related Questions