Import: why are single-export modules favored?

# Axel Rauschmayer (12 years ago)

When importing from a multi-export module, you need to wrap things in braces, which enables convenient syntax for single-export modules. Why? Have single-export modules in npm been counted? Are there more than multi-export modules?

I'm guessing that single-export modules will usually export functions, right?

# Sean Silva (12 years ago)

On Thu, Jul 4, 2013 at 7:43 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

When importing from a multi-export module, you need to wrap things in braces, which enables convenient syntax for single-export modules. Why? Have single-export modules in npm been counted? Are there more than multi-export modules?

I’m guessing that single-export modules will usually export functions, right?

In a sense, doesn't require()-style modules enforce only single-exports? Only a single value can be exported from a module, no?

I haven't been following the ES6 modules functionality super-closely, so I may be misinformed: My understanding is that the current ES6 proposed way to do module exports adds a new language construct for aggregating multiple values and passing them to another part of the program (namely, that all export'ed declarations are aggregated in a new way and made available to import statements, which use new language constructs to access the values aggregated by the module (import foo from ..., module foo from ...) and renaming values (as)).

-- Sean Silva

# Axel Rauschmayer (12 years ago)

In Node.js, the difference is:

module.exports = function () { … };

versus

module.exports = {
    foo: …,
    bar: …,
    baz: …
};

So, conceptually, the former piece of code exports a single value, while the latter exports multiple values.

Axel

# Sean Silva (12 years ago)

On Thu, Jul 4, 2013 at 5:05 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

In Node.js, the difference is:

module.exports = function () { … };

versus

module.exports = {
    foo: …,
    bar: …,
    baz: …
};

So, conceptually, the former piece of code exports a single value, while the latter exports multiple values.

I would say (in my purely anecdotal experience) that "most" of the well-written and idiomatic node modules that I have used use the former technique. In a lot of cases, the exported function is a "basic, 95% of use cases" functionality, and then the module exposes a richer, more advanced API by adding attributes to the function object.

Here are a couple examples: isaacs/minimatch, substack/dnode, mikeal/request

-- Sean Silva

# Ryan Florence (12 years ago)

Have single-export modules in npm been counted?

A look at npm would not be a representative sampling of JavaScript modules.

NPM houses library code, which lends itself more toward multi-export than application code does, in my experience anyway.

Every client app I've worked on has some sort of object model based on constructors (MooTools, jQueryUI, Backbone, Ember, lots of others). It would be silly to export anything but the constructor function from each module.

# Axel Rauschmayer (12 years ago)

OK, good. Favoring single-export modules over multi-export modules seems like the right call, then.