Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodeIntegration true ignored?

Tags:

electron

I'm trying to use a require directive in my js file attached to an html file. However, I am getting a require not defined error message with that file. My understanding is in newer versions of Electron after 5.0 that nodeIntegration was disabled by default. However, even after enabling nodeIntegration in web preferences I still am getting the require error message. My understanding was this nodeIntegration should solve that issue. Why am I still running into not defined issues with require? Here's the relevant section of the main.js file.

EDIT: I was missing a comma between preload and nodeIntegration, so thanks to the individual who pointed that out! However, I am still experiencing the issue. Still getting "Uncaught ReferenceError: require is not defined".

Second EDIT: Here's a minimal reproduction of the issue. The require function is undefined even when nodeIntegration is set to override it. In the end I'm just trying to read from a local json file but every instance I can find to do so in a simplistic manner when working with electron uses a require in some way, whether it is requiring fs or requiring the file. If the require statement won't function at all I can't make either work.

main.js:

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// 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: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration:true
    }
  })
  // and load the index.html of the app.
  mainWindow.loadFile('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 macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  // On macOS 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.

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
    <link href="main.css" rel="stylesheet">
    <title>Destiny Guide</title>
   <script src="problem.js"></script>
  </head>
  <body>
    <h1>Hello World</h1>
    Hello World
    <br>
    <h1><a href = "HelloWorld">helloWorld</a><h1>

    <!-- You can also require other files to run in this process -->
    <script src="./renderer.js"></script>
  </body>
</html>

problem.js:

var dataArc = require("./ZeroHourArc.json");

Third, and Last, EDIT: see answer below.

like image 356
Rob B. Avatar asked Dec 02 '22 09:12

Rob B.


1 Answers

Just try to add contextIsolation: false like this:

mainWindow = new BrowserWindow({
  width: 500,
  height: 500,
  icon: './public/logo.png',
  backgroundColor: 'white',
  webPreferences: {nodeIntegration: true, contextIsolation: false}
});
like image 74
Роман Avatar answered Dec 04 '22 07:12

Роман