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

  1. Appreciating the dedication you put into your blog and in depth
    information you present. It’s awesome to come across a blog
    every once in a while that isn’t the same out of date rehashed material.
    Great read! I’ve saved your site and I’m including your RSS
    feeds to my Google account.

  2. It’s actually a cool and useful piece of information. I am glad that you simply shared this
    helpful information with us. Please stay us up to date like this.
    Thanks for sharing.

  3. Unquestionably believe that which you said. Your favorite justification appeared to be on the
    internet the easiest thing to be aware of.
    I say to you, I certainly get annoyed while people think about worries
    that they plainly do not know about. You managed to hit the nail upon the top and
    also defined out the whole thing without having
    side-effects , people can take a signal. Will likely be back to get more.
    Thanks

  4. Hmm it looks like your blog ate my first comment (it was extremely long) so I guess I’ll just
    sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog writer but I’m still new to the whole thing.

    Do you have any recommendations for newbie
    blog writers? I’d certainly appreciate it.

  5. Here’s a little drama-filled tale about an Italian businessman I recently heard about, whose name we’ll spin as Gianni.
    He never expected crypto to become part of his job, yet destiny had different plans.
    He runs a company that receives payments from different countries, and traditional banking kept failing him.

    Everything went south one stressful afternoon. His business nearly froze because a payment didn’t go through, and his entire workflow collapsed for days.
    He said he had never felt that much pressure in business before.

    Clients got nervous, and he had to find a different solution fast.

    That’s when someone mentioned Paybis to him, he was unsure whether it was the
    right path. But the situation was too critical to ignore.
    So he checked the platform.

    To his surprise, things started moving quickly.
    Everything was explained clearly and simply. He could finally
    convert and transfer funds without drowning in paperwork.
    He later told me the relief was unbelievable.

    But the story doesn’t end here. The bank eventually apologized,
    but Paybis had already done the job the
    system failed to handle. He realized that sometimes innovation is the only path forward.

    Today, he uses Paybis whenever his business needs fast, clean, and predictable transactions.

    He’s not obsessed with crypto — he’s grateful for a platform
    that did what it promised.

    In the end, he said it straight: Paybis did a great job with his aim.

  6. Hi there! I could have sworn I’ve been to this blog before but after going through many of
    the articles I realized it’s new to me. Nonetheless,
    I’m certainly delighted I discovered it and I’ll be bookmarking it and checking back
    often!

  7. مع التوسع في الأسواق المالية والزيادة المستمرة
    في قائمة الأدوات المتاحة للتداول، نجد أن
    هناك اهتماماً مستمراً من جانب المستثمرين في الإمارات.
    هؤلاء المستثمرون يسعون دائماً للحصول على تحديثات فورية حول أسعار الفضة والذهب، وأي تغييرات قد تطرأ على الأسواق بهدف اتخاذ قرارات فعالة.
    إن فهم ديناميكيات السوق ومتابعة الأخبار
    الاقتصادية يُعتبران من أسياسيات النجاح في تداول الفوركس
    والمقايضات في الأسواق المالية العالمية.

  8. Hey there, I think your blog might be having browser compatibility
    issues. When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, wonderful blog!

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>