How to handle circular dependencies with RequireJS/AMD?

This is indeed a restriction in the AMD format. You could use exports, and that problem goes away. I find exports to be ugly, but it is how regular CommonJS modules solve the problem: define(“Employee”, [“exports”, “Company”], function(exports, Company) { function Employee(name) { this.name = name; this.company = new Company.Company(name + “‘s own company”); }; … Read more

Load “Vanilla” Javascript Libraries into Node.js

Here’s what I think is the ‘rightest’ answer for this situation. Say you have a script file called quadtree.js. You should build a custom node_module that has this sort of directory structure… ./node_modules/quadtree/quadtree-lib/ ./node_modules/quadtree/quadtree-lib/quadtree.js ./node_modules/quadtree/quadtree-lib/README ./node_modules/quadtree/quadtree-lib/some-other-crap.js ./node_modules/quadtree/index.js Everything in your ./node_modules/quadtree/quadtree-lib/ directory are files from your 3rd party library. Then your ./node_modules/quadtree/index.js file will just … Read more

Relation between CommonJS, AMD and RequireJS?

RequireJS implements the AMD API (source). CommonJS is a way of defining modules with the help of an exports object, that defines the module contents. Simply put, a CommonJS implementation might work like this: // someModule.js exports.doSomething = function() { return “foo”; }; //otherModule.js var someModule = require(‘someModule’); // in the vein of node exports.doSomethingElse … Read more