How can I use a Mongoose already opened connection inside an npm module that resides in /node_modules?
For instance:
app.js trigger opens a connection by requiring common/db.js and executing a connect() method.
Later in app.js a call is made to a_model/index.js where is required the a_model.js file and then is issued a findOne() call that never gets executed since it detects that no connection is open so the call is enqueued forever and never executes.
I already checked that the connection is already opened by checking the logs so the issue is other.
It's worth to note that I was able to get a similar setup working fine but instead of having a_module to be a npm module it was a regular directory below the app folder.
SOLUTION
Finally I got this working properly with the help of the awesome guys below.
By using the global nodejs object I was able to expose the db connection without clutter the code:
db.js:
global.db = mongoose;
a_model.js:
mongoose = global.db;
and voilá!
Reference: http://productbuilder.wordpress.com/2013/09/06/using-a-single-global-db-connection-in-node-js/