davidpauljunior
davidpauljunior

Reputation: 8338

Should you always `module.export` a JS module?

I've seen the following in a few places recently and wondering why you'd need to export an empty objcet?

module.exports = {};

As an example, a module is a polyfill and only contains a self executing function. What does the module.exports = {} at the end do? Because the code seems to work with or without it (I can require the module as a dependency).

(function polyfillWindowLocationOrigin (location) {
    if (location.origin === undefined) {
        location.origin = location.protocol + '//' + location.host;
    }
})(window.location);

module.exports = {};

Note: We're using browserify.

Upvotes: 3

Views: 138

Answers (1)

Danyal Aytekin
Danyal Aytekin

Reputation: 4215

If using the CommonJS module system then it can be assumed that this code appears at the start of each module:

var module = {
    exports: {}
};

So setting module.exports = {}; accomplishes little technically, but may be preferable in terms of explicitly documenting the fact that the module exports nothing.

Upvotes: 1

Related Questions