6

I making a regster form in HTML, with javascript. I Have file Login.js. My browser says the following error: Uncaught ReferenceError: require is not defined.enter image description here

isaque
  • 87
  • 1
  • 1
  • 3

3 Answers3

7

require is a way for different compilers to put in stuff that is needed, later on, while keeping the file small. Did you copy the js file from somewhere else?

You may need to include http://requirejs.org/

Also, this looks incredibly insecure. Are you planning to release this site to the word? or is it just for class/experiment?

As the one comment mentions, if this is supposed to be server-side code, it's not going to work very well on the client side.

Adam Pine
  • 387
  • 2
  • 7
4

require is defined in Node.js. Browsers don't have definition for require.

You need to use node module browserify to compile code that uses require for browsers.

Alternatively you can use RequireJS which is file and module loader for browsers.

And remember that normally you can't access files on your disk with javascript due to security reasons.

Tymski
  • 61
  • 3
  • This was my other concern... the fact he's calling file write events in JS... i've never done that before, lol. Makes me think he's trying to use node.js or something... very odd. – Adam Pine Aug 16 '17 at 22:03
2

I see this Q is 4 years old but I do not see an accepted answer.
I come across a similar issue in my electron app and solve it as below. I am posting my solution for future queries of others.

Adding webPreferences: {nodeIntegration: true} to BrowserWindow object may solve this problem.

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

if you're using electron >= version 12 use contextIsolation: false as well.

new BrowserWindow({
    webPreferences:{
        nodeIntegration:true,
        contextIsolation: false
    }
});
Ozgun Senyuva
  • 466
  • 5
  • 12