Circular dependencies

Circular dependencies don't work. Well, kinda they do. But not really. Inherently if a is defined in terms of b, b can't be defined in terms of a, but if that's not quite the relationship of of your cycle, you might make it (scarily) work.

// Module a var b = require('./circular-b-1') module.exports = 2 + b

// Module b var a = require('./circular-a-1') module.exports = a + 1 console.log(module.exports)

When we run module b we get "2[object Object]1", which seems like a peculiar answer. What happens is we start executing module b, we get to the require statement, so we switch over to executing module a. We get to the require statement, but since a is already started, and requires only execute once and then share the result, the default (empty) exported object is available.

JavaScript naturally adds 2 to the empty object, forming "2[object Object]" which is now available as the a module to b. b resumes execution, tacking a '1' onto the end of our mess before we log it.

So keep your module dependency tree a tree, and don't let cycles of requires develop. And keep the odd failure mode in mind so it doesn't take you forever to debug if you do get your shoelaces tied together.