2015-09-28 84 views
-1

我正在学习如何create/import/export模块Node.js, 我已经通过了these,并试图通过创建一个示例应用程序来学习。 我从根文件夹(Poc1)"npm install requirejs"发出command,并在Start.html中包含文件require.js未捕获的ReferenceError:模块/要求没有定义

,当我在浏览器中我得到打开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) 

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> 

回答

-1

好吧,我认为那些出口是不正确的:

exports.sayHelloInEnglish = function(){ 
    return "Hello"; 
} 

exports.sayHelloInSpanish = function(){ 
    return "Hola"; 
} 
+0

谢谢你的答案。我改变了建议的路径,现在得到以下错误: 未捕获的ReferenceError:未定义导出。 未捕获错误:模块名称“greetings.js”尚未加载上下文:_。使用require([]) –

+0

我想让downvoters解释动机。 – sailens

+0

@SunilThakur在你的HTML文件中,尝试在'' >'看看它是否能解决它。 – sailens

相关问题