简体   繁体   中英

Module.exports in NodeJS, outputting 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);

WHAT THIS OUTPUTS: [function]

I want it to output "exported" as in the module1.js class. What can I do?

Extra:

When I have this, in modulegetter.js ,

var f = function (input, input2) {

    return "exp";

};

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

It outputs exp as required, but why doesnt it work with module.exports?

您实际上需要调用该函数(带有或不带有参数):

console.log(module1());

module.exports exposes method or variable outside which can be accessed from other file . Imagine you have two files a.js and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM