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.

- 87
- 1
- 1
- 3
-
5You can't run Node.js server-side code in a browser. – SLaks Aug 16 '17 at 21:46
-
have you downloaded and implemented requirejs? useful documentation can be found here: http://requirejs.org/docs/start.html – hiquetj Aug 16 '17 at 21:46
-
6Please post **code**, not **screenshots**. – Claies Aug 16 '17 at 21:50
3 Answers
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.

- 387
- 2
- 7
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.

- 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
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
}
});

- 466
- 5
- 12