title: Global variable vs. require.cache in NodeJS
body: <p>I know using the <code>global</code> variable is discouraged in node, but assuming I need to store some data globally, which is a worse approach:</p>
  <ol>
  <li><p>using <a href="http://nodejs.org/api/globals.html#globals_global" rel="noreferrer">global</a></p>
  </li>
  <li><p>Using the <a href="http://nodejs.org/api/globals.html#globals_require_cache" rel="noreferrer">require cache</a> directly to store an object, i.e.</p>
  <p><code>require.cache[&quot;myGlobalVar&quot;] == {};</code></p>
  </li>
  </ol>
  <p>I suspect that option 2 is worse because the require cache is intended to be used for modules.</p>
  <p>More details:
  I thought about using require(&quot;myLibrary&quot;).myGlobalVar but that would require having myLibrary accessible to all the files calling it, which might not be possible in practice. I'm making a code coverage tool so I can expect the user to install &quot;myLibrary&quot; in their dev/test modules, but not necessarily in their src node_modules directory, to which the instrumented code files would refer.</p>