From CommonJS Spec Wiki
Jump to: navigation, search

setExports function discussion:

Namespaces proposal

The current specification makes it hard for modules to share or extend common namespace. This is handled much better e.g. in Google Closure Library, although at cost of using global object.

Therefore I propose the extension to the Modules standard in the form of optional second parameter of require function with the meaning of namespace object to be extended by required module, thus to be exposed to required module as exports objects, e.g.:

exports.f1 = function() {};
exports.f2 = function() {};
var namaspace1 = {};
require('modA', namespace1);
require('modB', namespace1);

This could be beneficial especially for modules using submodules, e.g.:

exports.f1 = function() {};
require('submodule1', exports);
require('submodule2', exports);
var module = require('module');

Exporting objects by modules could be more flexible if exporting would have been defined in terms of export function and not the exports object.