This is because you have a circular dependency. Node.js handles this in a very specific way:
-
The first module loads and runs (in this case,
book.js). It (book.js) will load and run the second module (author.js) when it (book.js) requires the other (author.js) -
When the second module (
author.js) is loaded and run, it (author.js) requires the first module (book.js) but it (author.js) will receive a partially filled object – however many things were set on the exports in book.js before it required author.js will be in that object -
After
book.jsis completely run through, the objectauthor.jsgot fromrequire('./book')will be the fullbook.jsmodule object
For more info, here’s the docs: http://nodejs.org/api/modules.html
If its possible to dynamically add that schema to one of those ActiveRecord objects, that’s one way to solve this. This is actually kind of a tricky situation. In fact, even without the module system, this would cause problems for you. If you put all this code in one file, how would you make it work?