Home > Software design >  require is not defined in electron
require is not defined in electron

Time:10-09

I'm trying to learn electron framework, but I'm having some problems accessing jQuery in my index.html. The error I get is:

'require()' is not defined

I have installed jQuery with npm:

npm install jquery --save

Than I tried to use it in the head of my index.html:

window.$ = window.jQuery = require("jquery");

As far as I understand, there are some security issues with direct access to node modules? I would really appriciate if someone can show me a proper way how to use jquery.

CodePudding user response:

Add nodeIntegration: true and contextIsolation: false to webPreferences in BrowserWindow. Like that.

let mainWindow = new BrowserWindow({
        ..........
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        },
});
  • Related