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

  1. I’m impressed, I have to admit. Seldom do I come across a blog that’s equally educative and engaging, and let me tell you,
    you’ve hit the nail on the head. The issue is an issue
    that not enough people are speaking intelligently about.
    I’m very happy I came across this in my hunt for something concerning this.

  2. When someone writes an piece of writing he/she maintains the plan of a user in his/her brain that how a
    user can know it. Therefore that’s why this paragraph is perfect.
    Thanks!

  3. 확실히 당신이 언급한 것을 생각합니다.
    당신의 가장 좋아하는 정당화는 웹에서 가장 단순한 요소로 기억해야 할 것처럼 보였습니다.
    저는 당신에게 말합니다, 저는 다른 사람들이 그들이 분명히 모르는 걱정를 생각할 때 짜증이 나 확실히 느낍니다.
    당신은 부작용 없이 전체을 깔끔하게 정의하고, 최고을 정확히 맞췄습니다.
    다른 사람들이 신호를 받을 수 있습니다.
    아마 더 얻기 위해 다시 올 것입니다.

    감사합니다

    Wow, superb blog layout! How long have you been running a
    blog for? you made blogging look easy. The total glance of your site is fantastic,
    let alone the content!

  4. Hi there, after reading this remarkable piece of writing i am as well cheerful to share my experience here
    with friends.

  5. Everyone loves what you guys are usually up too.
    This sort of clever work and exposure! Keep up the good works guys I’ve you guys
    to blogroll.

  6. What i don’t understood is actually how you’re not really much more smartly-favored than you might be now.
    You’re very intelligent. You know therefore significantly in the
    case of this matter, made me in my view imagine it from numerous varied
    angles. Its like women and men aren’t involved except it is one
    thing to do with Girl gaga! Your personal stuffs nice. At all times take care
    of it up!

  7. Hmm is anyone else encountering problems with the images on this
    blog loading? I’m trying to figure out if its a problem on my
    end or if it’s the blog. Any responses would be greatly
    appreciated.

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>