我正在学习如何create/import/export
模块在Node.js
,
我已经经历过these http://www.sitepoint.com/understanding-module-exports-exports-node-js/并试图通过创建示例应用程序来学习。
我发出了command
从根文件夹 (Poc1)"npm install requirejs"
,并包含文件require.js
in the Start.html
.
当我打开时Start.html
在浏览器中我得到 -
“未捕获的引用错误:未定义要求”,“未捕获的引用错误:未定义模块”。
我不确定我犯了什么错误,或者我需要包含哪些其他文件才能使其正常工作?
以下是示例应用程序的文件夹结构
Poc1 folder has these files and folders (greetings.js, main.js, Start.html, node_modules)
Poc1\node_modules has (requirejs\require.js)
grreetings.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>