-1

i gone through a video link to create a simple java script based app using electron. https://www.youtube.com/watch?v=TnXz_nnQZrwI

While trying to create an app i got the following error even though i enable the nodeIntegration to true

index.html:12 Uncaught ReferenceError: require is not defined at index.html:12

I don't know how to resolve it plz help This is my js and Html files

my index.js file

const { app, BrowserWindow } = require('electron');
const os = require('os-utils');
const path = require('path');

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences:{
      nodeIntegration: true
    }
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();
  os.cpuUsage(function(v){
    console.log('CPU Usage (%): '+ v*100);
    mainWindow.webContents.send('CPU',v*100);
    console.log('Mem Usage (%): '+ os.freememPercentage()*100);
    mainWindow.webContents.send('Mem',os.freememPercentage()*100);
    console.log('Total Mem (GB): '+ os.totalmem()/1024);
    mainWindow.webContents.send('Total-Mem',os.totalmem()/1024);
  });
};

// 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, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X 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 (BrowserWindow.getAllWindows().length === 0) {
    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 import them here.

my index.html file

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <link rel="stylesheet" href="index.css">
  </head>
  <body>
    <h1> Hello World!</h1>
    <p>Welcome to your Electron application.</p>
    <script>
      const electron = require('electron');
      const ipcRenderer = electron.ipcRenderer;

      ipcRenderer.on('CPU',(event,data)=>{
        console.log('CPU %' + data);

      });
      ipcRenderer.on('Mem',(event,data)=>{
        console.log('Mem Usage %' + data);

      });
      ipcRenderer.on('Total-Mem',(event,data)=>{
        console.log('Total Mem (GB)' + data);

      });
    </script>
  </body>
</html>
V_J viji
  • 39
  • 1
  • 7
  • Your index JavaScript file configures the integration but your HTML file isn't pulling that in before you run your inline script but I could be missing something – Aluan Haddad Apr 30 '21 at 16:24

2 Answers2

1

For Electron >= 12, nodeIntegration: true won't work without contextIsolation: false.

https://www.electronjs.org/docs/breaking-changes#default-changed-contextisolation-defaults-to-true

Erick
  • 1,138
  • 8
  • 15
0

Alternatively, if you do not want to take any security risks by setting nodeIntegration: true, you can make ipcRenderer available to your renderer process by creating a global variable in a "preload-file" that is called just before your renderer window is created.

In main.js create your main renderer window like this:

function createWindow () {
  win = new BrowserWindow({
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      contextIsolation: false
    }
  })
  win.loadFile('index.html')
}

The important part is preload: path.join(__dirname, 'preload.js').

In your preload.js you create a global variable ipcRenderer like this:

process.once('loaded', () => {
  global.ipcRenderer = electron.ipcRenderer;
});

Then ipcRenderer can be used in your renderer process' html code just like that:

<script>
...
ipcRenderer.send(...);
...
</script>
bassman21
  • 320
  • 2
  • 11