2013-01-09 90 views
2

我想在模块“B”的模块“A”中使用一些代码,但我不知道该怎么做。在属于另一个模块的模块中使用代码

我想这样做:

a.js

module.exports = { 
    hello : function(){ 
    alert('helo world'); 
    } 
}; 

b.js

module.exports = { 
    start : function(){ 
    alert(A.hello()); 
    } 
}; 

main.js

A = require("a"); 
B = require("b"); 
B.start(); 

,但我得到“A是没有定义的”。

谢谢!

回答

4

节点模块都有自己的范围,所以你也需要requireAb.js

var A = require('a'); 
module.exports = { 
    start : function(){ 
    alert(A.hello()); 
    } 
}; 
+3

因为'main.js'从不使用A,所以'main.js'中也不需要'require'('a')'。 – hunterloftis

相关问题