Writing universal modules
We have already written many of our own modules as part of our application. We can also write library modules for use in other applications.
When writing code for use by others, it's worth considering in what contexts it will be useful. Some libraries are only useful in specific environments. For example, Express is server-specific and jQuery is browser-specific. But many modules provide functionality that would be useful in any environment, for example, utility modules such as the uuid
module we've used elsewhere in this book.
Let's look at writing a module to work in multiple environments. We'll need to support more than just Node.js-style modules. We'll also need to support client-side module systems such as RequireJS. Recall from Chapter 4, Introducing Node.js Modules, that Node.js and RequireJS implement two different module standards (CommonJS and Asynchronous Module Definition (AMD), respectively). Our package may also be used client...