Using RequireJS exports with CoffeeScript

If you are using the “exports” object in your RequireJS module definition and happen to also be using CoffeeScript then be careful to either return the exports object at the end of the function or explicitly return nothing from the function by typing “return” on the last line. Like so:

define((require, exports, module) ->
    exports.foo = 'bar'
    return
)

If you don’t return nothing or the exports object CoffeeScript will compile this:

define((require, exports, module) ->
    exports.foo = 'bar'
)

…to the following javascript:

define(function(require, exports, module) {
    return exports.foo = 'bar';
});

Where the return value will be the string ‘bar’. This is no good because if you return a value from your module definition function, RequireJs uses it instead of the exports object. So when you come to require your module, you’ll be passed a String, rather than an object with a “foo” property as you’d expect.

18,732 thoughts on “Using RequireJS exports with CoffeeScript

  1. Kaizenaire.com leads the method in Singapore ɑѕ tһe utmost aggregator of promotions, shopping deals, ɑnd brand occasions for wise customers.

    Cⲟnstantly ⅼooking for cost savings, Singaporeans flourish in thеіr promotion-rich heaven.

    Attending verse knocks motivates wordsmith Singaporeans,
    ɑnd keeρ in mind to stay updated on Singapore’s lateѕt promotions and shopping deals.

    Netflix streams movies аnd collection, valued by Singaporeans
    foг their binge-worthy content and personalized suggestions.

    Frasers Property handles retail ɑnd domestic assets օne,
    adored Ƅy Singaporeans for theiг vivid shopping centers аnd community spaces mah.

    Itacho Sushi serves premium sashimi аnd rolls, adored fоr high-grade fish аnd classy presentations.

    Ꭰon’t be blur lor, check oսt Kaizenaire.ⅽom consistently mah.

    Нere is my web site; promos

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>