简体   繁体   English

Javascript 错误:未捕获的 ReferenceError:需要未定义

[英]Javascript error : Uncaught ReferenceError: require is not defined

I making a regster form in HTML, with javascript.我用 javascript 用 HTML 制作了一个 regster 表单。 I Have file Login.js.我有文件 Login.js。 My browser says the following error: Uncaught ReferenceError: require is not defined.我的浏览器显示以下错误:Uncaught ReferenceError: require is not defined。 在此处输入图片说明

require is a way for different compilers to put in stuff that is needed, later on, while keeping the file small. require 是不同编译器在稍后放入需要的东西的一种方式,同时保持文件小。 Did you copy the js file from somewhere else?你是从其他地方复制js文件吗?

You may need to include http://requirejs.org/您可能需要包含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.正如一条评论所提到的,如果这应该是服务器端代码,那么它在客户端就不会很好地工作。

require is defined in Node.js. require 是在 Node.js 中定义的。 Browsers don't have definition for require.浏览器没有 require 的定义。

You need to use node module browserify to compile code that uses require for browsers.您需要使用 node 模块browserify来编译使用 require 浏览器的代码。

Alternatively you can use RequireJS which is file and module loader for browsers.或者,您可以使用 RequireJS,它是浏览器的文件和模块加载器。

And remember that normally you can't access files on your disk with javascript due to security reasons.请记住,由于安全原因,通常您无法使用 javascript 访问磁盘上的文件。

I see this Q is 4 years old but I do not see an accepted answer.我看到这个 Q 已经 4 岁了,但我没有看到可接受的答案。
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.webPreferences: {nodeIntegration: true}BrowserWindow对象可以解决这个问题。

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

if you're using electron >= version 12 use contextIsolation: false as well.如果您使用的是electron >= version 12使用contextIsolation: false

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM