Skip to content

Instantly share code, notes, and snippets.

@lperrin
Created July 5, 2013 12:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lperrin/5934098 to your computer and use it in GitHub Desktop.
Save lperrin/5934098 to your computer and use it in GitHub Desktop.
A quick example of circular dependency with node.js. This code will crash unexpectedly because module A is only partially loaded by C.
// a.js
var moduleB = require('./b');
function ModuleA() {
}
ModuleA.hello = function () {
console.log('hello!');
};
module.exports = ModuleA;
// b.js
var moduleC = require('./c');
function ModuleB() {
}
ModuleB.hello = function () {
moduleC.hello();
};
module.exports = ModuleB;
// c.js
var moduleA = require('./a');
function ModuleC() {
}
ModuleC.hello = function () {
moduleA.hello();
};
module.exports = ModuleC;
// main.js
var moduleA = require('./a'),
moduleB = require('./b');
moduleB.hello();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment