title: Typescript path-mapping not being translated in node app
body: <p>Currently using typescript in a node.js application. I've compiled my app, and I'm using <a href="https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping" rel="noreferrer">typescript's path mapping feature</a>.</p>
  
  <p>'api/*' should instead look in the root folder. Using the following in development works:</p>
  
  <pre><code>nodemon --watch src -e ts,tsx --exec ts-node -r tsconfig-paths/register --disableWarnings ./src/index.ts
  </code></pre>
  
  <p><code>tsconfig-paths/register</code> allows the require to be translated correctly and ts-node will execute/run the app properly. Now, the problem comes happens when I move to production. In the past in production, I simply ran <code>tsc</code> on the folder, and moved the contents of the outDir (dist/) to /app in my docker image, and ran <code>node /app/index.js</code>. This has worked until I started using the path mapping feature of typescript. Now I simply get the error:</p>
  
  <blockquote>
    <p>Error: Cannot find module 'api/module/path/here'</p>
  </blockquote>
  
  <p>From <a href="https://github.com/Microsoft/TypeScript/issues/9910#issuecomment-234729007" rel="noreferrer">this github comment</a> the module names are apparently not mapped to the output compiled javascript.</p>
  
  <p>My tsconfig.json file:</p>
  
  <pre><code>{
      "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "jsx": "react",
        "allowJs": true,
        "alwaysStrict": true,
        "sourceMap": true,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitAny": false,
        "noImplicitThis": false,
        "strictNullChecks": false,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "lib": ["es2017", "dom"],
        "baseUrl": "src",
        "outDir": "dist",
        "types": [
          "node"
        ],
        "paths": {
          "universal/*": ["../../universal/*"],
          "api/*": ["*"],
          "*": ["node_modules/*"]
        },
        "typeRoots": [
          "./node_modules/@types",
          "./src/types"
        ]
      },
      "include": [
        "src/**/*"
      ]
  }
  </code></pre>
  
  <p>What is the recommended approach to using relative path-mapping in a node.js environment? If typescript is the one doing the resolution, why shouldn't it rewrite the require statements? It feels silly to involve another step like babel or webpack just to add the functionality that typescript is providing with module resolution.</p>
  
  <p>EDIT: After additional digging, I found that I could use <code>-r tsconfig-paths/register</code> in my node environment (I just have to copy in my tsconfig.json file). I can switch my entrypoint in docker to:</p>
  
  <pre><code>ENTRYPOINT [ "node", "-r", "tsconfig-paths/register", "index.js" ]
  </code></pre>
  
  <p>The problem is, I now need to modify my <code>tsconfig.json</code> baseUrl, as the directory <code>src/</code> doesn't exist. Also, I noticed that the resolution isn't working for the module 'util' (It's apparently using util in my node_modules folder instead of the node.js util library), which is causing my app to crash.</p>