node cached export object is broken

I'm requiring an module that has already been required in another file, but when I get the object, many of it's keys are undefined. A bunch of the keys are there, but some are not, even after I call some of the exposed functions. If I console.log the object, the keys look like they are there.

Example:

var mod = require('myModule')
console.log(mod) // { 'aKey':[Function], 'doStoff':[Function] }
console.log(mod.aKey) // undefined
mod.doStoff() // calls 'aKey' 
//TypeError: Object #<Object> has no method 'aKey'
//this error is thrown inside myModule

This would not overly surprise me if this module was doing something goofy when it get's loaded, but the module has already been loaded and I have used it for several things already in the main js file.

The only thing I can think of that is off the norm: I've required this module in the main js file (and it works), but the place that it does not work is inside a module that is required in the main.

To be clearer->

  • main.js requires myModule
  • main.js requires otherModule
  • otherModule requires myModule

myModule was installed by npm, but I'm prototyping the otherModule in place in the node_modules folder until I get it put up somewhere so npm will install it.

I don't see why this would cause a problem; I thought that the object that gets returned from require gets kept in the require cache--as is--so that when it gets required any time afterwards you get that same object. It seems that my object has been partially deleted or something.

What is causing this? What should I do differently?

I have found why I was having these issues:

When node tries to resolve the path for require('myModule', it will walk down the directory path from the position of the current module. If you have a symbolic link as a directory in your node_modules folder, nodejs will not follow that link back to your own node_modules folder--it will just walk down the directory path looking for your module. I had a symbolic link in there to help me keep my modules that are under development in a place I could easily test them. I'm not sure at what point I put that link in, but I didn't think it was important.

Since the require did not resolve to the same path for me, I kept getting a fresh load of the module, instead of the one in the require cache.

I am not sure why I got the symptoms I did originally (with certain keys being missing sometimes), but since I had a broken setup I guess I found some unexpected behavior.