close

[Solved] Electron require() is not defined

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Electron require() is not defined in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Electron require() is not defined Error Occurs?

Today I get the following error Electron require() is not defined in javascript.

How To Solve Electron require() is not defined Error ?

  1. How To Solve Electron require() is not defined Error ?

    To Solve Electron require() is not defined Error app.on('ready', () => {
    mainWindow = new BrowserWindow({
    webPreferences: {
    nodeIntegration: true,
    contextIsolation: false,
    }
    });
    });

  2. Electron require() is not defined

    To Solve Electron require() is not defined Error app.on('ready', () => {
    mainWindow = new BrowserWindow({
    webPreferences: {
    nodeIntegration: true,
    contextIsolation: false,
    }
    });
    });

Solution 1

As of version 5, the default for nodeIntegration changed from true to false. You can enable it when creating the Browser Window:

app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });
});

Solution 2

Are you using nodeIntegration: false while BrowserWindow initialization? If so, set it to true (defaults value is true).

And include your external scripts in the HTML like this (not as <script> src="./index.js" </script>):

<script>
   require('./index.js')
</script>

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment