circular require

394 views
Skip to first unread message

Irakli Gozalishvili

unread,
Dec 16, 2009, 5:14:50 PM12/16/09
to comm...@googlegroups.com
Hi all,

Other day I managed to obtained circular require between modules with narwhal and I had a hard time figuring out what was going on there. I looked at the wiki and have not found any note regarding how should require act in that situation. What about adding a line there, saying that it should throw some kind of error ??
  
--
Irakli Gozalishvili
Web: http://rfobic.wordpress.com/
Address: Taksteeg 3 - 4, 1012PB Amsterdam, Netherlands

Tom Robinson

unread,
Dec 16, 2009, 5:37:15 PM12/16/09
to comm...@googlegroups.com
Circular dependencies should work fine in Narwhal and any other
CommonJS-compliant module system.

The only issue (until we have module.exports/setExports) is if you
access a property which has not yet been defined in the module. e.x.

foo.js:

require("bar");
exports.baz = function() { print("ok"); }

bar.js:

var foo = require("foo");
foo.baz();

If you do require("foo") from another file the following happens:

1. foo.js is loaded, foo exports object created, and begins to be
executed
2. require("bar") gets called
3. bar.js is loaded, bar exports object created, and begins to be
executed
4. require("foo") is called
5. the module system see foo has already been loaded and is being
executed, so it returns foo's exports object
6. exports.baz has not yet been defined so you'll get an exception

Is this the problem you're experiencing? If not can you give me an
example?

-tom
> --
>
> You received this message because you are subscribed to the Google
> Groups "CommonJS" group.
> To post to this group, send email to comm...@googlegroups.com.
> To unsubscribe from this group, send email to commonjs+u...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/commonjs?hl=en
> .

Irakli Gozalishvili

unread,
Dec 16, 2009, 9:10:07 PM12/16/09
to comm...@googlegroups.com
Hmm,

Apparently issue was bit different http://github.com/tlrobinson/narwhal/issues/#issue/55
Yeap, I do see your point, cause we do create module first and then we're trying to fulfill it. I guess in my case it was force require as it was happening during bootstrap so require was forced to fulfill exports on each require call, that's why I had infinit require ping-pong.

Sorry everyone for starting this useless thread. Thanks Tom. 
Reply all
Reply to author
Forward
0 new messages