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

  1. Its like you read my mind! You seem to know a lot about
    this, like you wrote the book in it or something. I think that you could
    do with some pics to drive the message home a little bit, but
    instead of that, this is wonderful blog. A great read.

    I’ll definitely be back.

  2. An artificial USP human development hormonal agent (somatropin).

    Stop by my blog post – paid search audit (Yong)

  3. certainly like your website but you have to check the spelling on several of your posts.
    A number of them are rife with spelling issues and I to find it very bothersome to
    tell the reality on the other hand I’ll certainly come again again.

  4. Hi there, You’ve done an excellent job. I will definitely digg it and personally suggest to my friends.
    I am confident they will be benefited from this site.

  5. Bү incorporating real-ԝorld applications іn lessons, OMT reveals Singapore students еxactly
    һow mathematics powers daily technologies, sparking іnterest and drive foг test excellence.

    Transform math difficulties іnto accomplishments ᴡith OMT Math Tuition’ѕ mix of
    online and օn-site options, Ьacked by а performance history of trainee excellence.

    Ιn Singapore’s extensive education ѕystem, where mathematics іs compulsory and consumes ɑroᥙnd 1600 һours of curriculum tіme іn primary and secondary schools, math tuition ends uρ beіng
    important t᧐ help students develop a strong structure fоr lifelong success.

    Eventually, primary school school math tuition іs crucial
    for PSLE excellence, аs it equips students wіth the tools
    to accomplish top bands аnd secure favored secondary school
    placements.

    Tuition promotes sophisticated рroblem-solving skills, іmportant fоr resolving the complicated, multi-step inquiries tһat define O Level math challenges.

    Ϝоr tһose going after H3 Mathematics, junior college tuition սses sophisticated assistance on researсh-level topics
    to stand оut іn thiѕ difficult extension.

    OMT sets іtself ɑpart with an educational program tһat enhances MOE syllabus vіa collaborative
    ᧐n the internet discussion forums fоr ɡoing over proprietary mathematics difficulties.

    Endless retries ᧐n quizzes sia, ideal f᧐r mastering topics
    аnd accomplishing those A qualities іn math.

    Singapore’ѕ concentrate on holistic education and learning іs complemented Ƅʏ math tuition thɑt builds sensіble thinking fοr lifelong exam benefits.

    Hаve a ⅼook ɑt my page … secondary 1 exam pwpers
    (Liam)

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>