exports are a reasonable way of expressing module dependencies and interfaces, and the tools for managing modular code are getting better all the time.
Much less of a consensus has developed around the best way to deliver CommonJS modules to a web browser, where the synchronous semantics of
require pose a non-trivial implementation challenge. This module loader contributes to that confusion, yet also demonstrates that an amply-featured module loader need not stretch into the hundreds or thousands of lines.
npm install install
cd path/to/node_modules git clone git://github.com/benjamn/install.git cd install npm install .
When evaluated, the contents of install.js create a global function called
install. This function is the only external interface to the module loader, and it can be called in two ways.
The first way is to pass a module identifier string followed by a module factory function:
install"some/module/id"// CommonJS module code goes here.// For example:return setTimeoutcallback 0;;;
This makes the module available for requirement, but does not evaluate the contents of the module until the first time another module calls
The second way to invoke
install is to omit the module identifier and pass an anonymous module factory function:
install// Code that uses require goes here.// For example:require"some/module/id"setImmediateconsole.log"setImmediate fired";;;
Anonymous modules are executed in order of installation, as soon as their requirements have been installed. Note that such modules do not have exports objects, because anonymous modules cannot be required.
If a named module has no requirements and does not need its own scope, the following shorthand can be used to install the module:
install"simple/module" exports:one: 1two: 2buckle: "my shoe";