What do "module.exports" and "export.methods" mean in NodeJS / Express?

Looking at the random express source structure file for NodeJS , there are two lines of code that I don’t understand (these lines of code are typical of almost all NodeJS files).

 /** * Expose `Router` constructor. */ exports = module.exports = Router; 

and

 /** * Expose HTTP methods. */ var methods = exports.methods = require('./methods'); 

I understand that the first part of the code allows the rest of the functions in the file to work with the NodeJS application , but I don’t understand exactly how this works , or what the code in the line means.

What do exports and module.exports ?

I believe the second part of the code allows the available functions in the methods file, but again, exactly how it does it.

Basically, what are these magic words: module and exports ?

+55
javascript module export express
May 24 '11 at 21:13
source share
5 answers

More specific:

module is a global scope variable inside a file.

So, if you call require("foo") , then:

 // foo.js console.log(this === module); // true 

It acts just like window does in the browser.

There is also another global object called global that you can write and read from any file you want, but this is due to a change in the global scope, and this is EVIL

exports is a variable that lives on module.exports . This is basically what you export when a file is required.

 // foo.js module.exports = 42; // main.js console.log(require("foo") === 42); // true 

There is a small issue with exports on your own. Context Context _global + and module are not the same. (In the browser, the context of the global scope and window same).

 // foo.js var exports = {}; // creates a new local variable called exports, and conflicts with // living on module.exports exports = {}; // does the same as above module.exports = {}; // just works because its the "correct" exports // bar.js exports.foo = 42; // this does not create a new exports variable so it just works 

Export Details

+79
24 '11 at 22:03
source share
— -

To expand Raynos answer ...

exports is basically an alias for module.exports - I recommend just not using it. You can set methods and properties from a module by setting them to module.exports , as shown below:

 //file 'module1.js' module.exports.foo = function () { return 'bar' } module.exports.baz = 5 

Then you will access it in your code:

 var module1 = require('module1') console.log(module1.foo()) console.log(module1.baz) 

You can also override module.exports to just provide one object on demand:

 //glorp.js module.exports = function () { this.foo = function () { return 'bar' } this.baz = 5 return this // need to return `this` object here } 

You now have a good prototype:

 var g1 = new require('glorp')() console.log(g1.foo()) console.log(g1.baz) 

There are many other ways to play with module.exports and require . Just remember that require('foo') always returns the same instance , even if you call it several times.

Note

To perform the following steps

 var g1 = new require('glorp')() console.log(g1.foo()) console.log(g1.baz) 

this should be returned in the function assigned by module.exports . Otherwise, you get a TypeError :

 console.log(g1.foo()) ^ TypeError: Cannot read property 'foo' of undefined 
+34
May 26 '11 at 1:04 AM
source share

You can find the best answer in the source code for node.js. If someone needs your js module, your script turns into a node function as follows (see src / node.js).

 // require function does this.. (function (exports, require, module, __filename, __dirname) { ... your javascript contents... }); 

Node will transfer your script. Then the script will be executed as follows:

 //module.js var args = [self.exports, require, self, filename, dirname]; return compiledWrapper.apply(self.exports, args); 

So in the script,

 exports is just module.exports. 

In a script, you can add something to this export object (functions ..). The require function will return this object. This is node.js modular system (standard JS specification).

But be careful not to modify module.exports. Otherwise, your current export will be pointless.

+15
Nov 29
source share

module is an object that represents what I would like to publish in this particular source file. Instead of having something similar to the header files in the c / C ++ world, you describe what the module exports by defining this object. The node runtime uses this object to determine that your module is "publicly available."

its a similar concept for exporting functions from dlls in a compiled world. You must clearly define what functions the outside world can access. it helps with encapsulation and allows you to organize your libraries in a clean form.

+1
May 24 '11 at 10:05 pm
source share

The module code is in the module.exports file (a module can be compiled by another module). There are many ways to build a module, but this is one of the most common (and my personal favorite).

 // Dependencies // const module = require('module'); // Module object var foo = {} // Internal property foo._a = 'a'; // "Public" property foo.b = 'b'; // Method foo.fu = function() { return 'fu' }; // Export module.exports = foo; 
0
Jun 23 '19 at 22:55
source share



All Articles