在NodeJS中输出模块,输出function
module1.js
module.exports = function (input, input2) { return "exported"; };
modulegetter.js
var module1 = require('/Users/Aakarsh/Desktop/Node/ToDo/playground/module1.js'); console.log(module1);
这个输出是什么: [function]
我想要它输出"exported"
在module1.js
类。 我能做什么?
额外:
当我有这个时,在modulegetter.js
,
var f = function (input, input2) { return "exp"; }; console.log(f("f", "f"));
它根据需要输出exp
,但是为什么不能和module.exports一起使用?
你需要实际调用函数(带或不带参数):
console.log(module1());
module.exports
暴露外部可以从其他文件访问的方法或variables。 想象一下你有两个文件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
例