tobie/modulr-node — pull request #7 by phleet (2011-11-28 21:53:21+00:00)
Title: Report circular requires at runtime

Circular requires often manifest themselves are cryptic "undefined is
not a function" errors. This occurs when you have circular requires and
partially exported modules are being returned by `require`.

To fix this, we can keep track of the stack of requires and mark them as
being completely finished exporting by popping them off the stack. By
keeping track of this, we can see where exactly the circular requires
are taking place.

Here's a reduced example where the problem manifests itself.

```javascript
// foo.js

var hello = require('./bar').hello;

exports.world = function() {
console.log('world');
};

hello();
```

```javascript
// bar.js

var world = require('./foo').world;

exports.hello = function() {
console.log('hello');
world();
};
```
If this is built with `require('modulr').build('foo')` and run, it throws an `undefined is not a function` error.

With this change, before that error, it will also output

```
Circular Require: foo -> bar -> foo
```