【发布时间】:2015-09-28 06:23:47
【问题描述】:
我正在学习如何在Node.js 中使用create/import/export 模块,
我已经通过these 并试图通过创建示例应用程序来学习。
我从根文件夹(Poc1)"npm install requirejs" 发出了command,并将文件require.js 包含在Start.html 中。
当我在浏览器中打开 Start.html 时,我得到 -
"Uncaught ReferenceError: require is not defined", "Uncaught ReferenceError: module is not defined".
我不确定我犯了什么错误或我需要包含哪些其他文件才能使其正常工作?
下面的示例应用程序是文件夹结构
Poc1 folder has these files and folders (greetings.js, main.js, Start.html, node_modules)
Poc1\node_modules has (requirejs\require.js)
greetings.js 定义如下
module.exports.sayHelloInEnglish = function(){
return "Hello";
}
module.exports.sayHelloInSpanish = function(){
return "Hola";
}
main.js 定义如下
var greetings = require("./greetings.js");
function someFunc(){
var g1 = greetings.sayHelloInEnglish();
var g2 = greetings.sayHelloInSpanish();
alert(g1);
alert(g2);
}
Start.html 定义如下
<html>
<head>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="greetings.js"></script>
<script type="text/javascript" src="node_modules\requirejs\require.js"></script>
</head>
<body>
<span>Below is button</span>
<input type="button" onclick="someFunc()" value="clickMe"/>
</body>
</html>
【问题讨论】:
标签: node.js