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,610 thoughts on “Using RequireJS exports with CoffeeScript

  1. What you posted was actually very reasonable. But, think on this,
    suppose you added a little information? I ain’t
    suggesting your information isn’t solid, however what if you
    added a headline to maybe grab folk’s attention? I mean Using RequireJS exports with CoffeeScript |
    freestyle developments is kinda plain. You should look at
    Yahoo’s front page and see how they create news headlines
    to grab people to open the links. You might try adding a video or
    a pic or two to grab people excited about what
    you’ve got to say. In my opinion, it might make your posts a little livelier.

  2. Hey! Would you mind if I share your blog with my myspace group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Thanks

  3. I every time spent my half an hour to read this webpage’s
    articles or reviews everyday along with a cup of coffee.

  4. Thanks in support of sharing such a fastidious thinking, paragraph is nice,
    thats why i have read it fully

  5. I think that is one of the so much vital information for me.
    And i’m glad studying your article. However should remark
    on few common issues, The site style is perfect, the articles is in reality nice :
    D. Just right task, cheers

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>