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

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


module.exports is the object that's actually returned as the result of a require call.

The exports variable is initially set to that same object (i.e. it's a shorthand "alias"), so in the module code you would usually write something like this:

let myFunc1 = function() { ... };let myFunc2 = function() { ... };exports.myFunc1 = myFunc1;exports.myFunc2 = myFunc2;

to export (or "expose") the internally scoped functions myFunc1 and myFunc2.

And in the calling code you would use:

const m = require('./mymodule');m.myFunc1();

where the last line shows how the result of require is (usually) just a plain object whose properties may be accessed.

NB: if you overwrite exports then it will no longer refer to module.exports. So if you wish to assign a new object (or a function reference) to exports then you should also assign that new object to module.exports


It's worth noting that the name added to the exports object does not have to be the same as the module's internally scoped name for the value that you're adding, so you could have:

let myVeryLongInternalName = function() { ... };exports.shortName = myVeryLongInternalName;// add other objects, functions, as required

followed by:

const m = require('./mymodule');m.shortName(); // invokes module.myVeryLongInternalName


This has already been answered but I wanted to add some clarification...

You can use both exports and module.exports to import code into your application like this:

var mycode = require('./path/to/mycode');

The basic use case you'll see (e.g. in ExpressJS example code) is that you set properties on the exports object in a .js file that you then import using require()

So in a simple counting example, you could have:

(counter.js):

var count = 1;exports.increment = function() {    count++;};exports.getCount = function() {    return count;};

... then in your application (web.js, or really any other .js file):

var counting = require('./counter.js');console.log(counting.getCount()); // 1counting.increment();console.log(counting.getCount()); // 2

In simple terms, you can think of required files as functions that return a single object, and you can add properties (strings, numbers, arrays, functions, anything) to the object that's returned by setting them on exports.

Sometimes you'll want the object returned from a require() call to be a function you can call, rather than just an object with properties. In that case you need to also set module.exports, like this:

(sayhello.js):

module.exports = exports = function() {    console.log("Hello World!");};

(app.js):

var sayHello = require('./sayhello.js');sayHello(); // "Hello World!"

The difference between exports and module.exports is explained better in this answer here.


Note that the NodeJS module mechanism is based on CommonJS modules which are supported in many other implementations like RequireJS, but also SproutCore, CouchDB, Wakanda, OrientDB, ArangoDB, RingoJS, TeaJS, SilkJS, curl.js, or even Adobe Photoshop (via PSLib).You can find the full list of known implementations here.

Unless your module use node specific features or module, I highly encourage you then using exports instead of module.exports which is not part of the CommonJS standard, and then mostly not supported by other implementations.

Another NodeJS specific feature is when you assign a reference to a new object to exports instead of just adding properties and methods to it like in the last example provided by Jed Watson in this thread. I would personally discourage this practice as this breaks the circular reference support of the CommonJS modules mechanism. It is then not supported by all implementations and Jed example should then be written this way (or a similar one) to provide a more universal module:

(sayhello.js):

exports.run = function() {    console.log("Hello World!");}

(app.js):

var sayHello = require('./sayhello');sayHello.run(); // "Hello World!"

Or using ES6 features

(sayhello.js):

Object.assign(exports, {    // Put all your public API here    sayhello() {        console.log("Hello World!");    }});

(app.js):

const { sayHello } = require('./sayhello');sayHello(); // "Hello World!"

PS: It looks like Appcelerator also implements CommonJS modules, but without the circular reference support (see: Appcelerator and CommonJS modules (caching and circular references))