0

Require() is not working in script.js file (next to html and css files).

Are there any alternatives for .js file next to HTML and CSS

async function gen() {
  var child_process = require('child_process');
  child_process.execSync('npm install thealtening-free',{stdio:[0,1,2]});
  
  const bot = await altening.bot();
}

Image with error: Error: require is not defined

Can someone give me an alternative, that would work with HTML/CSS/JS combination

F. Müller
  • 3,969
  • 8
  • 38
  • 49
  • Are you using cjs? Try *.cjs. Require is a cjs thing (or older node versions). If you use esm standard you don't use require. – F. Müller Dec 27 '22 at 18:28

2 Answers2

0

I had this issue with an Electron app I created, this previous answer gave some help.

Electron require() is not defined

Edit:

The previous answer didn't really provide much information. I am sorry for that. If you are running Electron JS, possible answers could include changing nodeIntegration to true.

mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });

You could also try running npm install in your command line, to ensure all packages required by your application are installed correctly.

Lewis
  • 1
  • 2
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Barmar Dec 27 '22 at 18:29
  • 1
    Or just flag it as a duplicate. – Barmar Dec 27 '22 at 18:29
0

The code you are running is only compatible on the server side in NodeJS and similar technologies. There are two pieces to this that you need to understand.

  1. require is not supported on the web browser. In order to use it, you need a build tool for the web such as Parcel. There are a ton of other options, but I like Parcel. It also supports server-side builds and you can create shims for web, etc. for unsupported libraries. Please read up on that.

  2. child_process is only supported on the server side. It is a [NodeJS specific library][2] which means even if you used parcel to build for the web, it won't work unless you use a shim or potentially node emulation.

Nick
  • 4,002
  • 4
  • 30
  • 41