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.

310 thoughts on “Using RequireJS exports with CoffeeScript

  1. Aw, this was a really nice post. Spending some time and actual effort to produce a
    very good article… but what can I say… I put things off a lot and don’t
    manage to get anything done.

  2. Hello mates its enormous piece of writing regarding tutoringand entirely defined keep it up all the time. skla

  3. It’s difficult to find well-informed people in this particular topic, but you sound like you know what you’re
    talking about! Thanks

  4. В этой статье представлен занимательный и актуальный контент который заставит вас задуматься. Мы обсуждаем насущные вопросы и проблемы а также освещаем истории которые вдохновляют на действия и изменения. Узнайте что стоит за событиями нашего времени Узнать больше – https://vivod-iz-zapoya-1.ru/

  5. Somebody necessarily lend a hand to make severely posts I’d state.

    That is the very first time I frequented your website page and so far?

    I amazed with the research you made to create this actual
    post amazing. Magnificent job!

  6. Greetings! Very useful advice within this article!
    It’s the little changes which will make the greatest changes.
    Thanks for sharing!

  7. I blog often and I genuinely appreciate your information. Your article
    has really peaked my interest. I’m going to book mark
    your blog and keep checking for new details
    about once a week. I subscribed to your RSS feed as well.

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>