Writing Node Modules

In the browser, isolating scripts is a pain. Ultimately, every variable a script defines is either scoped in a function or attached to the global object, called window. Expressing dependencies from one script on another is a pain, too. How many times have you written if (window.x) ...? These problems are addressed by the proposed standard for ES6 modules, but it will take years before browser support is widespread.

Thankfully, Node has its own solution to this problem. Every file is its own module with isolated variable scope. There is a global object, simply called global (more semantic than window, wouldn’t you agree?), but it’s rarely used. Instead, each module attaches data it wants to share to a special object called ...

Get CoffeeScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.