title: SyntaxError: Cannot use import statment without a module, TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Catch 22
body: <p>I have been having issues with node js.</p>
  <p>Here is the relevant code (<code>app.ts</code>):</p>
  <pre><code>const express = require(&quot;express&quot;);
  const auth = require(&quot;./service/auth&quot;);
  
  import { Request, Response } from &quot;express&quot;;
  
  import User from &quot;./model/User&quot;;
  import con from &quot;./config/db&quot;;
  
  const port = 3000;
  
  const app = express();
  
  app.use(express.json());
  ...
  </code></pre>
  <p>The error I get is:</p>
  <pre><code>(node:16524) Warning: To load an ES module, set &quot;type&quot;: &quot;module&quot; in the package.json or use the .mjs extension.
  (Use `node --trace-warnings ...` to show where the warning was created)
  C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts:4
  import { Request, Response } from &quot;express&quot;;
  ^^^^^^
  
  SyntaxError: Cannot use import statement outside a module
      at wrapSafe (internal/modules/cjs/loader.js:1117:16)
      at Module._compile (internal/modules/cjs/loader.js:1165:27)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
      at Module.load (internal/modules/cjs/loader.js:1050:32)
      at Function.Module._load (internal/modules/cjs/loader.js:938:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
      at internal/main/run_main_module.js:17:47
  </code></pre>
  <p>So I look for a solution and find:
  <a href="https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module">SyntaxError: Cannot use import statement outside a module</a></p>
  <p>Which tells me to:</p>
  <blockquote>
  <p>Add &quot;type&quot;: &quot;module&quot; to the nearest parent package.json</p>
  </blockquote>
  <p>So add <code>&quot;type&quot;: &quot;module&quot;</code> to package.json. The output I get now is:</p>
  <pre><code>internal/modules/run_main.js:54
      internalBinding('errors').triggerUncaughtException(
                                ^
  
  TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension &quot;.ts&quot; for C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts
  [90m    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)[39m
  [90m    at Loader.getFormat (internal/modules/esm/loader.js:98:42)[39m
  [90m    at Loader.getModuleJob (internal/modules/esm/loader.js:227:31)[39m
  [90m    at async Loader.import (internal/modules/esm/loader.js:161:17)[39m {
    code: [32m'ERR_UNKNOWN_FILE_EXTENSION'[39m
  }
  </code></pre>
  <p>So again I go online, look for solution and find: <a href="https://stackoverflow.com/questions/62096269/cant-run-my-node-js-typescript-project-typeerror-err-unknown-file-extension">Can&#39;t run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension &quot;.ts&quot; for /app/src/App.ts</a></p>
  <p>Which tells me to:</p>
  <blockquote>
  <p>Remove &quot;type&quot;: &quot;module&quot; from package.json</p>
  </blockquote>
  <p>So I think I've found myself in a catch 22 situation. I'm not sure where to go next.</p>
  <p>I also tried the other solution in <a href="https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module">SyntaxError: Cannot use import statement outside a module</a></p>
  <blockquote>
  <p>Explicitly name files with the .mjs extension. All other files, such as .js will be interpreted as CommonJS, which is the default if type is not defined in package.json.</p>
  </blockquote>
  <p>This didn't work, either giving me the same error or giving me a different syntax error, I think due to the fact that I am using typescript.</p>
  <p>Thank you in advance to anyone who can help.</p>