2016-12-26 78 views
1

module1.jsModule.exports中的NodeJS,输出功能

module.exports = function (input, input2) { 

    return "exported"; 

}; 

modulegetter.js

var module1 = require('/Users/Aakarsh/Desktop/Node/ToDo/playground/module1.js'); 

console.log(module1); 

这是什么输出:[function]

我希望它如在t中输出"exported"module1.js类。我能做什么?

附加:

当我有这个,在modulegetter.js

var f = function (input, input2) { 

    return "exp"; 

}; 

console.log(f("f", "f")); 

它输出exp的要求,但为什么犯规它module.exports工作?

回答

2

您需要实际调用该函数(有或无PARAMS):

console.log(module1()); 
+0

是什么module1'和'模块1()''之间的差值Δθ – Marodian

+0

'module1()'执行/调用函数,'module1'只是对函数的引用,并返回函数本身,而不是它的结果/返回。 –

+0

谢谢!等待接受答案 – Marodian

1

module.exports暴露方法或变量之外,可以从其它文件访问。想象一下你有两个文件a.js和b.js。

a.js

var myfunction= function() { ... }; 
exports.myfunction= myfunction; 

b.js

var m = require('./a'); // require includes file by which function will be available 
m.myfunction(); // Once function is available you can call 
      //just like normal JavaScript function 

Example