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
CodePudding user response:
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.
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.
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.
CodePudding user response:
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.