title: Is it possible to modify exported variable after it is exported
body: <p>i'm still new to node.js and i'm trying to create and export my database connection with <a href="https://github.com/mysqljs/mysql" rel="nofollow noreferrer">mysqljs</a>.</p>
  
  <p>I'm having a trouble because if something happens like the database crash or a network problem, the connection needs to be re-created. I can't figure out how to udpate the previously exported connection to my app.</p>
  
  <p>Is there a way to update an object exported by the module like i did ?</p>
  
  <p>Am i missing something or using module.exports in the wrong way ?</p>
  
  <p>I think it would be possible to use a function or a constructor but i would have to edit every module that currently uses my dbh variable.</p>
  
  <pre><code>var reconnection_interval = 5000;
  var db_config = {
      host : "localhost",
      user : "root",
      password : "",
      database : "test"
  };
  
  var dbh;
  
  function handleDisconnect(conn)
  {
    conn.on('error', function(err)
    {
      // If the error is not fatal, we just ignore it.
      if(!err.fatal){return;}
      else
      {
          logger.error('Database error : '+err);
          logger.info('Trying to reconnect in '+reconnection_interval/1000+'s');
  
          // Destroying old connection.
          dbh = undefined;
  
          // Creating a new connection and trying to reconnect every &lt;reconnection_interval&gt;
          setTimeout(function(){
            dbh = mysql.createConnection(db_config);
            handleDisconnect(dbh);
            dbh.connect();
  
            // I want to update the module.exports value, so it becomes the new connection
            module.exports = dbh;
  
          }, reconnection_interval);
      } 
    });
  }
  
  
  dbh = mysql.createConnection(db_config);
  handleDisconnect(dbh);
  dbh.connect();
  logger.info("Connected to database.");
  
  
  module.exports = dbh;
  </code></pre>