Solution 1 :

Is there any error info in DevTool? like:

Uncaught ReferenceError: require is not defined.

If there are errors, you could enable nodeIntegration when open

BrowserWindow, for example 
    splashWindow = new BrowserWindow({
     width: 900,
     height: 600,
     show: true,
     frame: false,
     resizable: false,
     webPreferences: {
      nodeIntegration: true
     }
    });

Solution 2 :

Electron requires node.js. In your package.json project main entry point is where you declare:

const ElectronLIB = require("electron");

Problem :

Hello.

I am trying to develop an application using Electron.js. I have a problem. I need to require script tags in html. But when I do that, my other JavaScript code doesn’t work. What should I do?

Run:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My Application</title>
</head>
<body>

    <button id="tstbtn">TEST BUTTON</button>

<script>
//const ElectronLIB = require("electron");

let tstbtn = document.querySelector("#tstbtn");
tstbtn.addEventListener("click", () => {
    alert();
})

</script>

</body>
</html>

Dont’t run:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My Application</title>
</head>
<body>

    <button id="tstbtn">TEST BUTTON</button>

<script>
const ElectronLIB = require("electron");

let tstbtn = document.querySelector("#tstbtn");
tstbtn.addEventListener("click", () => {
    alert();
})

</script>

</body>
</html>

Note: When I call the require function later, click event is running.

Comments

Comment posted by question

Have a look at this

By