0

In https://github.com/jaredhanson/passport-local/blob/master/lib/index.js there is a construct as below:

 /**
    * Module dependencies.
   */

  var Strategy = require('./strategy');



/**
 * Expose `Strategy` directly from package.
 */
exports = module.exports = Strategy;

/**
 * Export constructors.
 */
exports.Strategy = Strategy;

It looks like Strategy is exported twice - directly and through the property Strategy - i.e. require('passport-local') and require('passport-local').Strategy both point to the same type. What is the purpose of such a construct?

Krishna
  • 1,871
  • 14
  • 26
  • possible duplicate of [module.exports vs exports in nodeJS](http://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-nodejs) – Ben Fortune Sep 10 '14 at 13:11
  • Ben, I understand that exports is a variable pointing to module.exports initially and if we alter module.exports we should also reassign exports. But my questions is specifically on the line "exports.Strategy = Strategy;" Here it is not a new object that is assigned to the Strategy property but the Startegy type itself, which is already exported. – Krishna Sep 10 '14 at 13:30
  • Not 100% sure but maybe this could be for legacy reasons? – leepowell Sep 10 '14 at 13:50
  • @leepowell, I think so too. I looked at passport-http module. That module exposes BasicStrategy and DigestStrategy. Please see my answer. – Krishna Sep 17 '14 at 12:55

2 Answers2

0

It creates a circular reference which backpoints to main object. It does not use extra memory or cause memory leak. It is the same object, just a reference to top. It's very commonly used in node.js (just log stream objects). Here apparently the library makes no use of it.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • Could you please point to any other source with similar construct where the property is used? – Krishna Sep 11 '14 at 18:36
  • @Krishna See https://github.com/joyent/node/blob/master/lib/_http_client.js#L457. If you log it you will something like this: http://stackoverflow.com/questions/17251553/nodejs-request-object-documentation – user568109 Sep 11 '14 at 22:36
0

Looking at it further, it looks like this is done to have consistency across various strategies. A module could export multiple strategies. So, modules will have a separate property for each strategy. But, in this case, the module contains a single strategy. So, it seems to have been exposed both as a the module and a property in the module, such that module.Strategy will return the strategy.

Krishna
  • 1,871
  • 14
  • 26