title: Javascript: How to require using const
body: <p>There's one thing I don't understand about modern Javascript. I see a lot of people discussing whether they should use <code>var</code>, <code>const</code>, or <code>let</code> when requiring new modules. Most people say it's <code>const</code> as their first priority and <code>let</code> second, but I don't see many people who are fan of <code>var</code>. However, this code down below would throw a <code>error TS2451: Cannot redeclare block-scoped variable 'other'</code> error. (<strong>Note</strong>: This error comes from the Typescript compiler, using the <code>commonjs</code> flag.)</p>
  
  <p><strong>main.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>'use strict';
  
  const A = require('./A.js');
  const B = require('./B.js');
  
  // do more stuff
  </code></pre>
  
  <p><strong>A.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>'use strict';
  const other = require('./other.js');
  
  class A {
      //...
  };
  
  module.exports = A;
  </code></pre>
  
  <p><strong>B.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>'use strict';
  const other = require('./other.js');
  
  class B {
      //...
  };
  
  module.exports = B;
  </code></pre>
  
  <p>I'm not sure in which cases it's error-less to use <code>const</code>. It seems that it only works when a module is imported in the main module using <code>const</code>, and then everything else in other modules have <code>var</code> for importing the same module. I'd like to know if I'm missing something. Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is the code of one of my modules. When I change the <code>var</code>s at the top to <code>const</code>, the error begins. I've also defined the same imports in other modules that are interconnected.</p>
  
  <pre><code>var Datastore = require('nedb');
  var validate = require("validate.js");
  var path = require('path');
  
  
  module.exports = class Planet{
  
      private db_filename : string  = "db/planets.db";
  
      private ds;
  
      private static instance : Planet = null;
  
      private constructor(){      
      }
  
  
      init(db_filename : string) : Planet{
          this.ds = new Datastore({ filename: db_filename, autoload: true, timestampData: true });
          return this;
      }
  
  
      static get_instance() : Planet{
          if(this.instance == null)
              this.instance = new Planet();
          return this.instance;
      }
  
  }
  </code></pre>