Home  >  Article  >  Web Front-end  >  How to Fix \"require() is not defined\" Error in Electron?

How to Fix \"require() is not defined\" Error in Electron?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 08:29:02770browse

How to Fix

Node Integration in Electron: Resolving "require() is not defined"

If you're encountering an error stating "'require()' is not defined" while attempting to utilize Node functionalities within your Electron HTML pages, this error typically arises due to the default setting of nodeIntegration being set to false in Electron version 5 and onward.

To resolve this issue and enable Node integration, you need to modify the settings while creating your Browser Window. The updated code snippet would look like this:

<code class="js">app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });
});</code>

By setting nodeIntegration to true, you grant the HTML pages access to Node's built-in modules, thereby resolving the "require() is not defined" error. This allows you to utilize variables such as:

<code class="js">var app = require('electron').remote; 
var dialog = app.dialog;
var fs = require('fs');</code>

in all your Electron HTML windows.

The above is the detailed content of How to Fix \"require() is not defined\" Error in Electron?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn