title: Nodejs, TypeScript, ts-node-dev & top-level await
body: <p>I have spent an hour now searching for a solution to a new error after needing top-level await. Everything else I have tried so far did not solve the error such as adding <code>&quot;type&quot;: &quot;module&quot;</code> to the package.json file.</p>
  <p>The message of the error is <code>Cannot use import statement outside a module</code> when starting the service.
  If I revert the change <code>&quot;module&quot;: &quot;ESNext&quot;,</code> to <code>&quot;module&quot;: &quot;commonjs&quot;,</code>, it works fine (except the await keywords have to be removed and somehow refactored to work without await).</p>
  <p>In addition, I use ts-node-dev to run the service which can be seen in the package.json file.</p>
  <ul>
  <li>The new package I need is kafkajs.</li>
  <li>node version: v14.9.0</li>
  <li>TypeScript version: 4.0</li>
  </ul>
  <p>package.json</p>
  <pre><code>{
    &quot;name&quot;: &quot;&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;microservice&quot;,
    &quot;main&quot;: &quot;src/index.ts&quot;,
    &quot;author&quot;: &quot;&quot;,
    &quot;type&quot;: &quot;module&quot;,
    &quot;license&quot;: &quot;MIT&quot;,
    &quot;scripts&quot;: {
      &quot;dev&quot;: &quot;NODE_ENV=development tsnd --respawn --files src/index.ts&quot;,
      &quot;prod&quot;: &quot;NODE_ENV=production tsnd --respawn --transpile-only --files src/index.ts&quot;,
      &quot;test&quot;: &quot;mocha --exit -r ts-node/register tests/**/*.spec.ts&quot;,
      &quot;eslint&quot;: &quot;eslint src/**/*.ts&quot;
    },
  </code></pre>
  <p>tsconfig.json</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      &quot;target&quot;: &quot;ES2020&quot;,
      &quot;module&quot;: &quot;ESNext&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot;,
      &quot;outDir&quot;: &quot;dist&quot;,
      &quot;sourceMap&quot;: true,
      &quot;esModuleInterop&quot;: true,
      &quot;allowSyntheticDefaultImports&quot;: true,
      &quot;experimentalDecorators&quot;: true,
      &quot;emitDecoratorMetadata&quot;: true
    },
    &quot;ts-node&quot;: {
      &quot;files&quot;: true,
      &quot;transpileOnly&quot;: true
    },
    &quot;include&quot;: [&quot;src/**/*.ts&quot;, &quot;declariations/**.d.ts&quot;],
    &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;.vscode&quot;]
  }
  </code></pre>