Home > Enterprise >  When I link JS script with HTML I get error: ReferenceError: require is not defined
When I link JS script with HTML I get error: ReferenceError: require is not defined

Time:12-03

I have an application written in the electron module. When I add a script from a JS file to an html file or enter a script between <script> tags and define a new module, I get an error: ReferenceError: require is not defined

This is my HTML script: <script src="../scripts/computer.js"></script>

This is the javascript file: const os = require("os")

I wanted to fetch information from my computer and then display it in my application using the "os" module but it doesn't work

CodePudding user response:

require is not native for browser, it is a part of Node.js

there is a good explanation in the answer to this post

how to use react require syntax?

CodePudding user response:

const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      ...
      nodeIntegration: true
      ...
    })

You can add 'nodeIntegration: true' to your window attribute in main.js

  • Related