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.

17,671 thoughts on “Using RequireJS exports with CoffeeScript

  1. Wonderful beat ! I would like to apprentice at the
    same time as you amend your web site, how can i subscribe for a weblog
    site? The account helped seo services near me a acceptable deal.
    I have been a little bit familiar of this your broadcast provided vivid transparent concept.

  2. Somebody necessarily lend a hand to make severely articles
    I would state. This is the very first time I frequented your web page and to this point?
    I amazed with the analysis you made to make this particular
    publish extraordinary. Excellent activity!

    Here is my blog … best seo company near me

  3. I do not even know the way I stopped up here, but I believed this
    put up was once good. I don’t understand who you’re but definitely you’re going to a well-known blogger in the event you are not already ;) Cheers!

    Also visit my blog post – best SEO services

  4. Send out these mailers out to promising neighborhoods.

    Here is my webpage … realtor postcards (Juan)

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>