title: Loading of port number for Nest.js application from JSON module
body: <p>In my Nest.js based application, I'm trying to use one configuration file to define all configuration for an application.</p>
  
  <p>The <strong>configuration.json</strong> file looks like this:</p>
  
  <pre><code>{
      "environment": "development",
      "backendPort": 8080,
      "appRootPath": ".",
      ...more configuration...
  }
  </code></pre>
  
  <p>And in my Nest.js application's <strong>main.ts</strong> is:</p>
  
  <pre><code>import { NestFactory } from "@nestjs/core";
  import configuration from "../configuration.json";
  import { AppModule } from "./app.module";
  
  
  
  async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      await app.listen(configuration.backendPort);
  }
  bootstrap();
  </code></pre>
  
  <p>I have enabled TypeScript's <strong>resolveJsonModule</strong> feature as mentioned in <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html" rel="noreferrer">TypeScript 2.9 Release notes</a> and VS code successfully recognized the import and provides IntelliSense and type checking.</p>
  
  <p>But when I try to start the app via</p>
  
  <pre><code>ts-node -r tsconfig-paths/register src/main.ts
  </code></pre>
  
  <p>I get an error:</p>
  
  <pre><code>(node:5236) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'backendPort' of undefined
      at d:\CodeDev\NestCMS\backend\src\main.ts:10:36
      at Generator.next (&lt;anonymous&gt;)
      at fulfilled (d:\CodeDev\NestCMS\backend\src\main.ts:4:58)
      at &lt;anonymous&gt;
      at process._tickDomainCallback (internal/process/next_tick.js:228:7)
      at Function.Module.runMain (module.js:695:11)
      at Object.&lt;anonymous&gt; (d:\CodeDev\NestCMS\backend\node_modules\ts-node\src\_bin.ts:177:12)
      at Module._compile (module.js:652:30)
      at Object.Module._extensions..js (module.js:663:10)
      at Module.load (module.js:565:32)
  (node:5236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:5236) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  
  <p><strong>Is there a way to define application port in an elegant way like I tried?</strong></p>
  
  <p>I don't want the port number to be hard-coded in the main.ts file. I want it to be configurable by some option to allow me to deploy the same application build to different environments where the only thing differing will be the configuration.json.</p>