I think as developers using ES modules we have all got to a point where import paths start to become ugly and unmaintainable - especially in large ...
For further actions, you may consider blocking this person and/or reporting abuse
Just a note about monorepos, look at
tsc -b package1/tsconfig.json package2/tsconfig.json
its helpful.IDEs might get confused with this, if you're using an editor worth its salt, it should be able to do auto imports anyway.
It's needed for the Typescript compiler to understand packages that sit in sibling arrangements that are not resolvable in node style resolution. I use webstorm which is salty haha.
Actually, it is resolved in VSCode, because it is also Microsoft's product.
But wouldn't Node complain after we transpile TS, as Node doesn't know about absolute paths?
tsc will automatically resolve it.
ts-node and ts-node-dev currently won't, but can be fixed with
-r tsconfig-paths/register
.Doesn't work. Created a simple example with absolute imports and then compiled it via
tsc
. On runningnode dist/index.js
the compiled JS throws an error as the absolute paths are not resolved.Let's see your example buddy?
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.
I have two files:
src/index.ts
src/utils/math.ts
My tsconfig.json - the rest is omitted
After transpiling this to JS, it doesn't work just by calling
node dist/index.js
, where dist is the output folder. So thebaseUrl
is just for TypeScript as Node doesn't understand that.Now I really need to compile to JavaScript as you mentioned.
I summed up the solution, but not exactly that pretty.
TypeScript, simplified import paths, and what you have to be careful
Pacharapol Withayasakpunt ・ Jul 23 ・ 2 min read
That's a good one, I used it before. Although, personal feeling, the barrels (index.ts) is a better approach.
You can use it with barrels 👍
Didn’t know about this, thanks!!