title: ts-node 'MODULE_NOT_FOUND' when using absolute imports in TypeScript
body: <p>I stumbled upon a problem with absolute imports. The repository is publicly available here: <a href="https://github.com/repetitioestmaterstudiorum/ts-boiler" rel="nofollow noreferrer">https://github.com/repetitioestmaterstudiorum/ts-boiler</a></p>
  <p>When I'm trying to import a file with absolute path (relative to the project directory) and then execute <code>npm run dev</code> or <code>npm run ts-node src/index.ts</code> I am getting the following error:</p>
  <pre><code>Error: Cannot find module '/src/constants'
  Require stack:
  - /Users/&lt;my-username&gt;/&lt;some-path&gt;/ts-boiler/src/index.ts
      at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
      at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/&lt;my-username&gt;/&lt;some-path&gt;/ts-boiler/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
      at Function.Module._load (node:internal/modules/cjs/loader:778:27)
      at Module.require (node:internal/modules/cjs/loader:1005:19)
      at require (node:internal/modules/cjs/helpers:102:18)
      at Object.&lt;anonymous&gt; (/Users/&lt;my-username&gt;/&lt;some-path&gt;/ts-boiler/src/index.ts:1:1)
      at Module._compile (node:internal/modules/cjs/loader:1105:14)
      at Module.m._compile (/Users/&lt;my-username&gt;/&lt;some-path&gt;/ts-boiler/node_modules/ts-node/src/index.ts:1597:23)
      at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
      at Object.require.extensions.&lt;computed&gt; [as .ts] (/Users/&lt;my-username&gt;/&lt;some-path&gt;/ts-boiler/node_modules/ts-node/src/index.ts:1600:12) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ '/Users/&lt;my-username&gt;/&lt;some-path&gt;/ts-boiler/src/index.ts' ]
  }
  </code></pre>
  <p>(my username and folder structure is obfuscated for privacy reasons)</p>
  <p>Relative imports, such as <code>import { C } from './constants'</code> inside the file <code>src/index.ts</code> work fine. When changing that to <code>import { C } from '/src/constants'</code> or <code>import { C } from 'src/constants'</code> with the corresponding <code>tsconfig.json</code> settings, I'm getting the error.
  (the same error also occurs when I append <code>.js</code> or <code>.ts</code> to the import)</p>
  <p><code>tsconfig.json</code> settings for absolute imports with leading dash enabled:</p>
  <pre><code>&quot;baseUrl&quot;: &quot;.&quot;,
  &quot;paths&quot;: {
      /* Support absolute imports with a leading '/' */
      &quot;/*&quot;: [&quot;*&quot;]
  },
  </code></pre>
  <p>I usually work with MeteorJS and recently followed the tutorial for Remix (to get to know the framework). Both of these frameworks encourage absolute imports and I copied both of their <code>tsconfig.json</code> files into my project (adding <code>~</code> in the case of Remix's settings) to try and see if their configuration would work for me - without success!</p>
  <p>I also looked at this on how to enable absolute imports: <a href="https://javascript.plainenglish.io/why-and-how-to-use-absolute-imports-in-react-d5b52f24d53c" rel="nofollow noreferrer">https://javascript.plainenglish.io/why-and-how-to-use-absolute-imports-in-react-d5b52f24d53c</a> which resulted in the same error.</p>
  <p>What's more confusing for me is that VSCode with ESLint configured does not complain about the absolute imports with the right settings in the <code>tsconfig.json</code> file.</p>
  <p>And weirdly, there is one import that uses an absolute path that works well inside the project with the same settings: <code>import type { Constants } from '/types/t.constants'</code>. It also works without &quot;type&quot;, e.g. <code>import { Constants } from '/types/t.constants'</code>. It could be because the imported file is not in &quot;src/&quot; but in &quot;types/&quot;?</p>
  <p>Maybe someone solved a similar problem once?</p>