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

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


To be more specific:

module is the global scope variable inside a file.

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

// foo.jsconsole.log(this === module); // true

It acts in the same way that window acts in the browser.

There is also another global object called global which you can write and read from in any file you want, but that involves mutating global scope and this is EVIL

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

// foo.jsmodule.exports = 42;// main.jsconsole.log(require("foo") === 42); // true

There is a minor problem with exports on it's own. The _global scope context+ and module are not the same. (In the browser the global scope context and window are the same).

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

Read more about exports


To expand on Raynos's answer...

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

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

Then you get access to it in your code:

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

You can also override module.exports entirely to simply provide a single object upon require:

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

Now you've got a nice prototype:

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

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

Note

For the following to work,

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

this has to be returned in the function that is assigned to module.exports. Otherwise, you'll get a TypeError:

console.log(g1.foo())          ^TypeError: Cannot read property 'foo' of undefined


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

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

Node will wrap your script. Then above script will be executed as follows:

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

So in your script,

exports is just module.exports.

In your script, you can add something to this exports object (functions..).require function will return this object. This is node.js's module system (commonJS specification).

But be careful not to modify module.exports. Otherwise your current exports will be meaningless.