Require returns an empty object

This is because you have a circular dependency. Node.js handles this in a very specific way:

  1. 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)

  2. 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

  3. After book.js is completely run through, the object author.js got from require('./book') will be the full book.js module 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?

Leave a Comment