title: "SyntaxError: Cannot use import statement outside a module" error in ts-node while modules are CommonJS
body: <p>The main cause of &quot;SyntaxError: Cannot use import statement outside a module&quot; error is non-CommonJS modules (<code>compilerOptions.module</code> in <strong>tsconfig.json</strong>), but I got same error with <code>&quot;module&quot;: &quot;CommonJS&quot;</code>.</p>
  <p>Initially, my <strong>tsconfig.json</strong> was:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;compilerOptions&quot;: {
  
      &quot;target&quot;: &quot;ES2020&quot;,
      &quot;module&quot;: &quot;CommonJS&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot;,
      &quot;allowSyntheticDefaultImports&quot;: true,
      &quot;esModuleInterop&quot;: true,
  
      &quot;strict&quot;: true,
      &quot;noUnusedLocals&quot;: true,
      &quot;noUnusedParameters&quot;: false,
      &quot;experimentalDecorators&quot;: true,
  
      &quot;baseUrl&quot;: &quot;./01-Source&quot;,
      &quot;paths&quot;: {
        &quot;@EnterpriseBusinessRules/*&quot;: [ &quot;./BusinessRules/Enterprise/*&quot;],
        // Other aliases
      }
    }
  }
  </code></pre>
  <p>but with this config, <strong>ts-node</strong> will not understand the aliases and it will be error</p>
  <pre><code>Error: Cannot find module '@EnterpriseBusinessRules/XXX/NNN'
  </code></pre>
  <p>Alhgough the ts-node does not understrand the alieses, it understands the <code>import</code>.
  To solve the aliaases issue, I added</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;compilerOptions&quot;: { /* */ },
    &quot;ts-node&quot;: {
      &quot;require&quot;: [
        &quot;tsconfig-paths/register&quot;
      ]
    }
  }
  </code></pre>
  <p>part to *<em>tsconfig.json</em> and now error is</p>
  <pre><code>import {
  ^^^^^^
  
  SyntaxError: Cannot use import statement outside a module
  </code></pre>
  <p>I know that inside <code>ts-node</code> group we can overwrite the <code>compilerOptions</code> like:</p>
  <pre><code>{
     &quot;ts-node&quot;: {
      &quot;compilerOptions&quot;: {
        &quot;module&quot;: &quot;CommonJS&quot;,
        &quot;esModuleInterop&quot;: true
      },
      &quot;require&quot;: [
        &quot;tsconfig-paths/register&quot;
      ]
    }
  }
  </code></pre>
  <p>but the <strong>module</strong> is already <strong>CommonJS</strong> and <strong>esModuleInterop</strong> is already <strong>true</strong>, so I must not to overwrite it (but even is to try, nothing change)</p>