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

  1. Банкротство с ипотекой — это возможность списать долги и при этом сохранить жилье при грамотном сопровождении. Переходите по запросу как подать на банкротство с ипотекой. Разберем, можно ли пройти процедуру с действующей ипотекой, как защитить квартиру, какие есть риски и последствия. Поможем подобрать законное решение именно под вашу ситуацию, включая случаи с единственным жильем, детьми и другими кредитами.

  2. به طور کلی

    برای اون دسته علاقه‌مندها

    فعالیت‌های شرطی

    میخوان شروع کنن

    این برند

    احتمالا گزینه باشه

    انتخاب خوبی باشه

    در ضمن

    برندهای شناخته‌شده‌ای مثل

    enfejarоnline آنلاین

    و

    سرویس sibbet

    تجربه خوبی ارائه دادن

    در پایان

    رضایت‌بخش بود

    و

    باز هم

    بازم سر میزنم

    My blߋg post; مرجع معتبر ایرانی

  3. Greetings from Colorado! I’m bored to tears at work so I
    decided to browse your website on my iphone during lunch break.
    I really like the information you provide here and can’t wait to take a look
    when I get home. I’m amazed at how quick your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyhow, great site!

    Also visit my site – Best Seo Services In Ahmedabad

  4. I blog often and I truly thank you for your information. This great article has truly peaked my interest.
    I will book mark your site and keep checking for new details about
    once a week. I opted in for 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>