title: What is the best way to load JSON from a dynamic filename in node.js & Typescript?
body: <p>I am creating a simple web application with node.js and Typescript to build my familiarity with it, and I want to know the best way to load a JSON config file whose filename is determined at runtime. </p>
  
  <p>I have found a few suggestions online:</p>
  
  <ul>
  <li><a href="https://hashnode.com/post/how-to-use-a-json-file-in-typescript-cix9q0z3i00wc6h536ovr1dde" rel="nofollow noreferrer">Using the fs module in node</a></li>
  <li><a href="https://hackernoon.com/import-json-into-typescript-8d465beded79" rel="nofollow noreferrer">Importing the JSON file like a module</a></li>
  </ul>
  
  <p>The first approach appears like it should work, but the second seems a lot neater. The problem is that with the second approach, I have the following error:</p>
  
  <pre><code>An import declaration can only be used in a namespace or module. ts(1232)
  </code></pre>
  
  <p>because I want to import the file in a constructor with a filename specified as an argument. Secondly I get the following error if I try to append the given filename to the constant directory I want to get it from:</p>
  
  <pre><code>';' expected. ts(1005)
  </code></pre>
  
  <p>Here is a (non-compiling) code snippet from the class in which I'm trying to load the JSON, and an example JSON file that I'm trying to load in.</p>
  
  <p>Typescript class:</p>
  
  <pre><code>import Floor from './Floor'
  import Elevator from './Elevator'
  import Person from './Person'
  
  class Building {
  
      public name: string
      private floors: Floor[]
      private elevators: Elevator[]
      private people: Person[]
      private time: number
  
      constructor(config_filename: string) {
          import * as config from '../config/'+config_filename
          const building = (&lt;any&gt;config).building
          this.name = name
          this.floors = []
          building.floors.forEach((floor) =&gt; {
              this.floors.push(new Floor(floor.number, floor.name))
          })
          this.elevators = []
          building.elevators.forEach((elevator) =&gt; {
              this.elevators.push(new Elevator(elevator.name, elevator.weight_capacity, elevator.start_floor_no, this))
          })
          this.people = []
          building.people.forEach((person) =&gt; {
              const person_instance = new Person(person.name, 10, person.algorithm)
              this.people.push(person_instance)
              this.floors[person.start_floor_no].addOccupant(person_instance)
          })
          this.time = 0
      }
  ...
  </code></pre>
  
  <p>Example JSON config file:</p>
  
  <pre><code>{
      "building": {
          "name": "Basic Inc.",
          "floors": [
              {
                  "number": 0,
                  "name": "Ground Floor"
              },
              {
                  "number": 1,
                  "name": "1st Floor"
              }
          ],
          "elevators": [
              {
                  "name": "Bruce",
                  "weight_capacity": 100,
                  "start_floor_no": 0
              }
          ],
          "people": [
              {
                  "name": "Wendy Fox",
                  "start_floor_no": 0,
                  "algorithm": "desk"
              }
          ]
      }
  }
  </code></pre>
  
  <p>Should I stick with the first approach, or is there some neater way to load the JSON file with a filename only known at runtime?</p>