The RequireJS library implements AMD with loading, bundling and
other functionality. It brings sensible imports (with
global namespace. Interestingly, as Node.js implements CommonJS-style
require’s, there are also support modules to bring Node.js code under AMD.
![AMD/RequireJS Talk][img_talk] [img_talk]: http://loose-bits.com/media/img/2012/11/15/nodedc-requirejs.png
Source Code and Demo
The talk and the source give a good overview and run-through of how to implement simple shared code, but to give a better sense of the final result, here is a RequireJS-compliant code module (for shuffling elements in a string) that has an underscore dependency, and works in both the browser and as a standard module in Node.js:
We add some boilerplate at the top of the library for Node.js, then use
define function to make a
require import function available to both the
browser and Node.js, and we’re off and code sharing!