Thursday, 01 July 2010
Modules that can be used both on the server and the client-side are a useful way to reuse code. This is one way of writing a module that will work well with both systems, while allowing you to write code in the familiar node style.
In node you use module.exports (or just exports) to expose a function:
However, in the browser this will break since 'exports' is not defined. So, to work around this, we first need to check if exports is defined, and if not, create a sensible object for exporting functions to. In the browser this could be a global with the same name as the module:
The second problem is that anything you define in the module but don't export can become available in the browser:
In the example above, requiring the module in node would return an object with the method test and nothing else, but in the browser this code would create a new global function called notExported. To fix this problem its a good idea to wrap the module's code in a closure (this is good practice anyway!).
Below is an example module that uses a closure which accepts an exports object as an argument. A sensible exports object is created if one does not already exist.
Note the use of this['mymodule'] as an export location in the browser. This code can now be used both client and server side: