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. Could someone explain to me how I can resolve this issue?
我看过requirejs,但我不确定为什么我会收到这个错误。有人可以向我解释我如何解决这个问题吗?
2 个解决方案
#1
9
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." that would explain why the symbol require
is not defined when the script runs. 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()
. requirejs is one such library that you could use.
所以当你说“我在html页面的头部的脚本标签中有这个代码时”。这可以解释为什么在脚本运行时没有定义符号require。如果要在浏览器中使用require(),则需要首先使用脚本标记加载定义require函数的库,并支持require()类型功能,并确保在尝试使用之前已成功加载要求()。 requirejs就是你可以使用的一个这样的库。
require()
is built into node.js on the server so it is always available there.
require()内置于服务器上的node.js中,因此它始终可用。
#2
4
Basically you need to transform your source code for the browser, replacing require calls with the actual module code. Take a look at these utilities:
基本上,您需要转换浏览器的源代码,将require调用替换为实际的模块代码。看看这些实用程序:
- http://browserify.org/ - it does exactly the transformation you need
- https://webpack.github.io/ - a more complicated web packaging framework, with lots of other useful features.
http://browserify.org/ - 它完全符合您的需要
https://webpack.github.io/ - 一个更复杂的Web打包框架,具有许多其他有用的功能。
#1
9
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." that would explain why the symbol require
is not defined when the script runs. 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()
. requirejs is one such library that you could use.
所以当你说“我在html页面的头部的脚本标签中有这个代码时”。这可以解释为什么在脚本运行时没有定义符号require。如果要在浏览器中使用require(),则需要首先使用脚本标记加载定义require函数的库,并支持require()类型功能,并确保在尝试使用之前已成功加载要求()。 requirejs就是你可以使用的一个这样的库。
require()
is built into node.js on the server so it is always available there.
require()内置于服务器上的node.js中,因此它始终可用。
#2
4
Basically you need to transform your source code for the browser, replacing require calls with the actual module code. Take a look at these utilities:
基本上,您需要转换浏览器的源代码,将require调用替换为实际的模块代码。看看这些实用程序:
- http://browserify.org/ - it does exactly the transformation you need
- https://webpack.github.io/ - a more complicated web packaging framework, with lots of other useful features.
http://browserify.org/ - 它完全符合您的需要
https://webpack.github.io/ - 一个更复杂的Web打包框架,具有许多其他有用的功能。