1

I have a confusion with module.exports. As far as I understood module.exports is an object that is exposed to other modules,

exports=module.exports={} 

But in the index.js of morgan package in node.js I found this.

module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token

morgan, compile, format and token are all functions.

Can you explain whats happening here? How is a function(morgan) assigned to module.exports ? After the first line is executed, is module.exports a function instead of an JSON object?

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78

4 Answers4

3

A Node module basically works like this:

var module = {
    exports: {}
};
(function (exports, require, module, __filename, __dirname) {

    // your module code here

})(module.exports, require, module, __filename, __dirname);

var exported = module.exports;

By default, exports, and module.exports both point to the same object. You can add properties to the object as normal. However, what if you want to return a function or some other object instead of just the default standard object?

In that case, you can set module.exports to something else, and that will be the new exported object.

module.exports = function() {};

And of course, that function can have properties too, so your code is kind-of like this:

module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};

Which would be equivalent to:

var morgan = function() {};
var compile = function() {};
var format = function() {};
var token = function() {};

morgan.compile = compile;
morgan.format = format;
morgan.token = token;
module.exports = morgan;

How is a function(morgan) assigned to module.exports ? After the first line is executed, is module.exports a function instead of an JSON object?

Yes, module.exports will be a function, in place of the default object (however there is no JSON here, JSON is not a JavaScript object, but an encoding format).

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
1

You might want to read this answer for a more in-depth explanation:

What is the purpose of node js module exports and how do you use it?

Morgan is simply adding parameters to the module being exported (in this case compile, format, and token. When you require the module in your application using something like morgan = require('morgan'), you can then call morgan.format to return the format that was appended to the object.

Hope that clears things up a bit!

Community
  • 1
  • 1
polskais1
  • 186
  • 9
0

module.exports is an object that is exposed to other modules and files. This allows a convenient way to export variables, functions and more javascript features.

As you can see here , when you initialize any variable to {} you are initializing an object.

An object can contain variables and functions, this is one of the things that makes Javascript really cool. You can easily pass complex objects with functions as parameters and do real nice clean code.

So, short story, think about module.exports as an object that is exposed to other modules and files with variables and functions. Just like passing an Object Oriented object as a parameter in Java or Ruby.

Here is easily explained: Read More

0

Can you explain whats happening here?

An assignment statement is happening.

// path-to-my-module.js
module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token

For each of these 4 statements, there is an assignment statement. Looking at the first line, module.exports = morgan, that would mean the module.exports object is going to have a property called morgan with its value being the value of the identifier morgan

So when you use it like this:

var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
Ryan
  • 14,392
  • 8
  • 62
  • 102