Node.js module.exports parent/child variable reference
<p>In node.js I have this scenario:</p>

<p>main.js</p>

<pre><code>module.exports = {
  dbHandler: {}
}

const DB_CONNECT = require('dbConnect.js');
const CHILD_MODULE = require('childModule.js');

module.exports.dbHandler = DB_CONNECT.connectDB(); // establishes the connection to the sqlite3 db

// ... give some time to module.exports.dbHandler to be loaded. (lab testing)

CHILD_MODULE.queryDB(); // &lt;----- error occurs
</code></pre>

<p>childModule.js</p>

<pre><code>   var db = module.parent.exports.dbHandler;
   //issue is here. Even after the parent have set dbHandler, this still empty {}.

module.exports.queryDB = function(){
  db.all('SELECT * from mytable', (err, rows) =&gt; { // callback
     console.log(rows);
  }
</code></pre>

<p>Since <code>DB_CONNECT.connectDB()</code> is async, I give it a while (lab test) to load the database and updating <code>module.exports.dbHandler</code> before calling <code>CHILD_MODULE.queryDB()</code>
the error occurs when <code>db.all</code> is called.</p>

<p><code>TypeError: db.all is not a function</code></p>

<p><code>db</code> still an empty object <code>{}</code>.
What is wrong in this code? How do I make the child's <code>db</code> to access the parent's <code>module.exports.dbHandler</code> ?</p>