title: NestJS startup unbelievably slow in development
body: <p>My application in dev environment is annoyingly slow in it's startup phase. I've set some debug logging on various places to see what is taking up so much time, and found that my <code>main.ts</code> actually uses almost <strong>9 minutes</strong>  just to get my <code>app.module</code> imported!</p>
  
  <p><strong>Source</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>import { performance } from 'perf_hooks';
  const startTime = performance.now();
  
  import { Log } from 'api/common/util/logger/log';
  Log.log.info(`┌────────────────────────────────────────────────────────────┐`);
  Log.log.info(`│    Starting: ${new Date().toISOString()}                      │`);
  Log.log.info(`└────────────────────────────────────────────────────────────┘`);
  
  // From here --------------------&gt;
  import { NestFactory } from '@nestjs/core';
  import { ValidationPipe } from '@nestjs/common';
  import 'reflect-metadata';
  import { existsSync, mkdirSync, writeFile } from 'fs';
  import * as express from 'express';
  import * as bodyParser from 'body-parser';
  import * as helmet from 'helmet';
  import * as morgan from 'morgan';
  import * as morganBody from 'morgan-body';
  // &lt;------ to here, imports fly in as expected.
  
  // Theese take a bit longer, but not enormously
  import { Config } from './api/common/config';
  import { HttpExceptionFilter } from './api/common/filters/http-exception.filter';
  import { LogService } from 'api/common/util/logger/log.service';
  
  // This one takes up the most time on startup (several minutes)
  import { AppModule } from './api/app.module';
  Log.log.debug(` * imports done in ${(performance.now() - startTime).toFixed(3)}ms`);
  Log.log.debug(` * Memory: ${readMem()}`);
  
  function readMem() {
    const mem = process.memoryUsage();
    const convert = { Kb: n =&gt; (n / 1024), Mb: n =&gt; convert.Kb(n) / 1024 };
    const toHuman = (n, t) =&gt; `${convert[t](n).toFixed(2)}${t}`;
    return `Used ${toHuman(mem.heapUsed, 'Mb')} of ${toHuman(mem.heapTotal, 'Mb')} - RSS: ${toHuman(mem.rss, 'Mb')}`;
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <p>Production startup:</p>
  
  <pre><code>$ node dist/main.js
  info: ┌──────────────────────────────────────────────────────────────────────────┐
  info: │    Starting: 2019-01-29T13:06:13.751Z                                    │
  info: │      Memory: Used 6.54Mb of 11.70Mb - RSS: 25.33Mb                       │
  info: │     Runtime: js                                                          │
  info: └──────────────────────────────────────────────────────────────────────────┘
  debug:  * imports done in 6862.350ms
  debug:  * Memory: Used 87.99Mb of 113.76Mb - RSS: 133.58Mb
  info: Nest application successfully started
  info: ┌──────────────────────────────────────────────────────────────────────────┐
  info: │             Memory: Used 93.71Mb of 122.52Mb - RSS: 144.20Mb             │
  info: │             Launch: 2019-01-29T13:06:25.377Z                             │
  info: │      Time to start: 11991.049ms                                          │
  info: │     Bootstrap time: 5124.189ms                                           │
  info: └──────────────────────────────────────────────────────────────────────────┘
  </code></pre>
  
  <p>Development startup:</p>
  
  <pre><code>$ ts-node -r tsconfig-paths/register src/main.ts
  info: ┌──────────────────────────────────────────────────────────────────────────┐
  info: │    Starting: 2019-01-29T13:08:06.914Z                                    │
  info: │      Memory: Used 157.76Mb of 193.62Mb - RSS: 209.77Mb                   │
  info: │     Runtime: ts                                                          │
  info: └──────────────────────────────────────────────────────────────────────────┘
  debug:  * imports done in 471159.063ms
  debug:  * Memory: Used 297.45Mb of 385.35Mb - RSS: 408.90Mb
  info: Nest application successfully started
  info: ┌──────────────────────────────────────────────────────────────────────────┐
  info: │             Memory: Used 216.64Mb of 383.35Mb - RSS: 409.11Mb            │
  info: │             Launch: 2019-01-29T13:16:05.521Z                             │
  info: │      Time to start: 483228.325ms                                         │
  info: │     Bootstrap time: 12042.239ms                                          │
  info: └──────────────────────────────────────────────────────────────────────────┘
  </code></pre>
  
  <p>Yes, I'm starting this using <code>ts-node</code>, but that is what NestJS recommends for development and debugging.</p>
  
  <h1>Question</h1>
  
  <p>How can I optimize the startup so that each minor change in the backend here does not require a 10 minute procrastination? I have trouble enough with concentration as it is, this does not help. </p>
  
  <p>Do I have too many modules? Will it help if I combine some? I have about 15 DB entity models each included in it's own graphql based module for readability, but many of them have circular dependencies resolved by <code>forwardRef()</code> injection in my module imports. Is this perhaps a problem? </p>
  
  <p>I try to include as few third-party libraries as possible to avoid node_modules hell. What I import in my modules are either my own code or NestJS framework stuff. Of course I do not know how many implicit dependencies gets loaded, but could the amount of libraries I'm dragging with me impact the startup performance? And if so, how can I monitor what gets on the stack and how much memory/cpu each script consumes upon evaluation? And can I somehow precompile some of this to increase startup? </p>
  
  <p>I don't have this problem when running as compiled javascript in production.</p>