2015-09-28 37 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

相關問題