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

The best answers to the question “What is the purpose of Node.js module.exports and how do you use it?” in the category Dev.


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

I can’t seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source code.

According to the Node.js documentation:


A reference to the current
module. In particular module.exports
is the same as the exports object. See
src/node.js for more information.

But this doesn’t really help.

What exactly does module.exports do, and what would a simple example be?


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:


var count = 1;

exports.increment = function() {

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()); // 1
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:


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


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

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


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');

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


Some few things you must take care if you assign a reference to a new object to exports and /or modules.exports:

1. All properties/methods previously attached to the original exports or module.exports are of course lost because the exported object will now reference another new one

This one is obvious, but if you add an exported method at the beginning of an existing module, be sure the native exported object is not referencing another object at the end

exports.method1 = function () {}; // exposed to the original exported object
exports.method2 = function () {}; // exposed to the original exported object

module.exports.method3 = function () {}; // exposed with method1 & method2

var otherAPI = {
    // some properties and/or methods

exports = otherAPI; // replace the original API (works also with module.exports)

2. In case one of exports or module.exports reference a new value, they don’t reference to the same object any more

exports = function AConstructor() {}; // override the original exported object
exports.method2 = function () {}; // exposed to the new exported object

// method added to the original exports object which not exposed any more
module.exports.method3 = function () {}; 

3. Tricky consequence. If you change the reference to both exports and module.exports, hard to say which API is exposed (it looks like module.exports wins)

// override the original exported object
module.exports = function AConstructor() {};

// try to override the original exported object
// but module.exports will be exposed instead
exports = function AnotherConstructor() {}; 


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:


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


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

Or using ES6 features


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


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))