To explain, I have an app who have a database class instance (dealing with the database) and a process manager (who organize different processes) and well many other modules need those 2 singletons to interact with, so for now I am passing those 2 class instances within a cascade to every other objects who need to interact with them.
But now I discovered I can do it another way, like mongoose is doing it : using module caching.
The idea is to not export the class but to export an instance of the class module.exports = new ClassName(); and then at the beginning of modules using that instance I can just do var myVar = import(‘module.file.path’); and use this global var in the module. It is working because of node.js caching the instance therefor every modules doing this import will get the same instance.
However I know global var is a bad word and it all depend on the node module caching which is less explicit than passing down my object instance but well if mongoose is built to be used that way I am thinking it might be not that bad ? and what is wrong of using all possibilities offered by node to reduce code lines and add simplicity ?
I would be happy to get your opinion on that subject.