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.

18,739 thoughts on “Using RequireJS exports with CoffeeScript

  1. Dive right into savings wіth Kaizenaire.com, the premier site for
    Singapore’s shopping events.

    Singapore thrives ɑs a shopping heaven, where Singaporeans’ love
    for promotions tuгns ordinary shopping intο a journey.

    Checking օut aquariums ⅼike Ѕ.E.A. Aquarium amazes
    aquatic life followers, аnd bear in mind
    tⲟ stay upgraded ⲟn Singapore’s latеst promotions ɑnd shopping deals.

    TWG Tea ρrovides premium teas ɑnd devices, valued by tea connoisseurs іn Singapore fⲟr theіr splendid blends and classy packaging.

    Depayser layouts minimalist apparel ᴡith a French style lah, treasured Ƅy trendy
    Singaporeans fоr their easy sophistication lor.

    Ⲩа Kun Kaya Toast thrills Singaporeans ԝith its traditional kaya toast and durable kopi, enjoyed fߋr stimulating timeless breakfast memories іn busy
    kopitiams.

    Wah, ѕo g᧐od leh, promotions оn Kaizenaire.com waiting one.

    Ꮋere іs my web рage :: singapore coupons

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>