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.

7,668 thoughts on “Using RequireJS exports with CoffeeScript

  1. Hurrah! Finally I got a web site from where I be able to in fact obtain valuable information regarding my study and
    knowledge.

  2. Heya i am for the first time here. I found
    this board and I find It truly useful & it helped me out much.
    I hope to give something back and help others
    like you aided me.

  3. Quality content is the secret to be a focus for the viewers to visit the web
    site, that’s what this web site is providing.

  4. A fascinating discussion is definitely worth comment. I do believe that you ought to publish
    more on this topic, it might not be a taboo matter but typically folks
    don’t discuss such issues. To the next! Cheers!!

  5. Having read this I thought it was very enlightening.
    I appreciate you spending some time and energy to put
    this content together. I once again find myself spending a lot of time both reading and leaving comments.
    But so what, it was still worthwhile!

  6. Hello, i think that i saw you visited my website so i came to “return the favor”.I’m attempting
    to find things to enhance my site!I suppose its ok to use a few of your ideas!!

  7. I have been surfing on-line greater than three hours today, yet I never discovered any
    fascinating article like yours. It is lovely price enough for me.
    In my view, if all site owners and bloggers made good content as
    you did, the internet might be much more useful than ever before.

  8. Hey there would you mind stating which blog platform you’re working with?
    I’m going to start my own blog soon but I’m having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs
    and I’m looking for something completely unique.
    P.S Sorry for getting off-topic but I had to ask!

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>