0

> index.html: As u can see in index.html code (const electron = require('electron');this require arise an error as require is not define i use window10.

<script>
const electron = require('electron');
const { ipcRenderer } = electron;

document.querySelector('form').addEventListener('submit',(event) =>{event.preventDefault();
event.preventDefault();
const file =document.querySelector('input').files[0];
});
</script>

> Main.js:

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true, // is default value after Electron v5
      contextIsolation: true, // protect against prototype pollution
      enableRemoteModule: false, // turn off remote
      preload: path.join(__dirname, "preload.js") // use a preload script
    }
  });

  
  mainWindow.loadFile('index.html')

  
  • since contextIsol is enabled, I believe you can only require inside of preload scripts not the renderer/index script – pushkin Nov 01 '21 at 18:10
  • 1
    i'm pretty sure there's a number of similar dupe issues on SO though - i'd advise you to search for them – pushkin Nov 01 '21 at 18:10

1 Answers1

0

Electron is a package you should use in the nodejs environment, not in the browser. Also in the browser you can't use the require function, you should use instead the import and export syntax.

That because javascript is a script language meant to run in browsers, it doesn't make sense to require electron in your frontend script, but, since your serving your files through the electron service and not in a browser window you can access the require by calling the window.require('pckg') method, still doesn't make any sense to require electron in a client-side window tho, it's like if an express server renders a page that creates another server.

If you just want to create another window you should do that in the main electron file, in your case the Main.js file.

eroironico
  • 1,159
  • 1
  • 15
  • 1
    _"it doesn't make sense to require electron in your frontend script"_ this is normally the case, though for Electron there's modules that can *only* be used from the renderer. Take a look at [this example](https://www.electronjs.org/docs/latest/api/ipc-main#sending-messages), notice how Electron's being required in the 2nd code block. – Joshua Nov 02 '21 at 00:35
  • 1
    Yeah, actually i messed up things, i created a couple of apps and never needed to send data to electron so i complitely forgot this thing, thank you for the explanation – eroironico Nov 02 '21 at 07:16