Last Updated: February 25, 2016
·
2.361K
· jaysoo

[RequireJS] Using exports in CoffeeScript modules

If you use RequireJS with exports in CoffeeScript, make sure the last line of your file is not:

exports = MyModule

OR

exports.Foo = Foo

Since CoffeeScript has implicit returns, that will get compiled down to:

return exports = MyModule

AND

return exports.Foo = Foo

This will sometimes result in situations where requiring that module will give you an empty object {}.

To fix this you can either:

1) Just use explicit return and avoid using exports.

2) Put and empty return at the end of your module.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside