DEV Community

Discussion on: How I simplified my import paths in TypeScript

Collapse
 
ryands17 profile image
Ryan Dsouza

Doesn't work. Created a simple example with absolute imports and then compiled it via tsc. On running node dist/index.js the compiled JS throws an error as the absolute paths are not resolved.

Thread Thread
 
andrewmcoupe profile image
Andy Coupe

Let's see your example buddy?

Thread Thread
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

It can be fixed via babel-plugin-module-resolver, though. Actually, Babel can do much more than tsc, and typescript can run under it as well.

Only just it doesn't help with the IDE.

Thread Thread
 
ryands17 profile image
Ryan Dsouza

I have two files:

src/index.ts

import { add } from 'utils/math'

console.log(add(2, 3))

src/utils/math.ts

export const add = (a, b) => a + b

My tsconfig.json - the rest is omitted

{
  "baseUrl": "./src",
  "paths": {
     "*": ["*"]
  }
}

After transpiling this to JS, it doesn't work just by calling node dist/index.js, where dist is the output folder. So the baseUrl is just for TypeScript as Node doesn't understand that.

Thread Thread
 
patarapolw profile image
Pacharapol Withayasakpunt

Now I really need to compile to JavaScript as you mentioned.

I summed up the solution, but not exactly that pretty.