[英]Resolve “Uncaught ReferenceError: require is not defined” error in Node.js
I am trying to set up a basic contact form using SengGrid and I keep getting a "Uncaught ReferenceError: require is not defined" error. 我正在尝试使用SengGrid设置一个基本的联系表单,并且我不断收到“未捕获的ReferenceError:require is not defined”错误。
I have this code in a script tag in the head of the html page. 我在html页面的头部的脚本标记中有这段代码。
var sendgrid = require('sendgrid')(username,pass);
I have looked at requirejs, but I am not sure why I am getting this error. 我看过requirejs,但我不确定为什么我会收到这个错误。 Could someone explain to me how I can resolve this issue? 有人可以向我解释如何解决这个问题吗?
require()
is not built into the browser. require()
未内置到浏览器中。
So when you say that "I have this code in a script tag in the head of the html page." 所以当你说“我在html页面的头部的脚本标签中有这个代码时”。 that would explain why the symbol require
is not defined when the script runs. 这可以解释为什么在脚本运行时没有定义符号require
。 If you want to use require()
in the browser, then you need to first use a script tag to load a library that defines the require function and supports the require()
type functionality and make sure that is successfully loaded before you try to use require()
. 如果要在浏览器中使用require()
,则需要首先使用脚本标记加载定义require函数的库,并支持require()
类型功能,并确保在尝试使用之前已成功加载require()
。 requirejs is one such library that you could use. requirejs就是你可以使用的一个这样的库。
require()
is built into node.js on the server so it is always available there. require()
内置于服务器上的node.js中,因此它始终可用。
Basically you need to transform your source code for the browser, replacing require calls with the actual module code. 基本上,您需要转换浏览器的源代码,将require调用替换为实际的模块代码。 Take a look at these utilities: 看看这些实用程序:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.