0

Here is a code snippet

 body.user_id = userObj._id;

          exports.inFCID(conn, obj.fcid, body, fcid, 0).then(function (r) {

            exports.getUserById(conn, body.user_id).then(function (u) {
              console.log("after getuserbyid", u);

Here I am sort of didn't understand why user have done something like

  exports.inFCID(conn, obj.fcid, body, fcid, 0).then(function (r 

of to be precise what does it do? I have previously encounter things like module.exports and export default statement ( export something statements) but this seems to be new.. Can someone explain me what this snippets would normally do? Ignoring what is inside those functions (inFCID) or what does export.something do in middle of the code..

  • that's very weird code and it is certainly not exporting anyting. It would only make any sense if exports had such properties assigned to it in another section in the same module. Even if that's the case, it is very confusing and should be refactored – Aluan Haddad Jul 30 '18 at 09:27
  • Same as `module.exports`, the method can be exported from any point in a file https://www.sitepoint.com/understanding-module-exports-exports-node-js/. But, in this case, the case doesn't seem to be the same. Here, more than one thing is exporting. – Abin Thaha Jul 30 '18 at 09:29
  • Those are invocations of previously assigned exports. Otherwise they're going to blow up. They don't export anything themselves – Aluan Haddad Jul 30 '18 at 09:30

2 Answers2

0

It's expected that inFCID export is defined in this module as well:

exports.inFCID = function inFCID (...) {...};

When an export is defined as function expression, inFCID function is not available as inFCID but as exports.inFCID.

Referring exported functions as exports.inFCID inside module is a common recipe to improve testability in CommonJS modules; the same recipe won't work with ESM; a module needs to be separated when used with ES modules, as explained in this answer. Module exports can be spied or mocked outside the module:

const foo = require('foo');
...
spyOn(foo, 'inFCID');
foo.bar();
expect(foo.inFCID).toHaveBeenCalled();

This would be impossible if inFCID(...) was referred directly.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
0

exports is a regular object.

If you have a code like this:

function test() {}

module.exports.test = test

Then nodejs will convert it into something like this:

function moduleInvocation(module, exports) { 
  function test() {}
  module.exports.test = test
}

// a rough dummy code, to illustrat what node does on require
function require(moduleName) {
   var module = { exports : {} };

   //
   //  some code that loades `moduleName` and wraps it into `moduleInvocation`
   //

   moduleInvocation(module, module.exports)

   return module;
}

So if someone writes exports.inFCID() then it is not different to:

var obj = {};
obj.inFCID = function() {}

obj.inFCID();

But it does not make any sense to write it that way except if you compose the exports of the module out of the content of some sub files.

t.niese
  • 39,256
  • 9
  • 74
  • 101