I've started an application in typescript. I've learned about some features of TS and it looked like something, that could help me, hovewer, I don't work like I would expect. The documentation of TS says something about Path mapping in Module resolution chapter. This module mapping can, if I understand it correctly, save me few or even lot of double-dots in import
. So I have created a "inc" directory with one file (at this time), that will be included in multiple files in multiple directories. Into tsconfig I've written following:
"baseUrl": ".",
"paths": {
"@inc/*": [ "inc/*" ]
}
Now, I would expect, that using import { X } from "@inc/somefile"
(even from file resting somewhere deep in folder tree) would result in importing that export X
from ./inc/somefile.ts (or .js, when runnning).
Hovewer, the compiler/transpiler will leave the import
statement intact, so when I try to run this code using node.js
, it will die because there is no @inc/somefile
- node doesn't read tsconfig and tsc doesn't create any mapping functions.
I can, of course, read and parse the path
element by hand in some kind of require wrapper, but I believe there is something I'm doing wrong and/or better way to achieve this.
Thank you for your replies.
Remember that
if you then try to exeute the compiled files with node (or ts-node), it will only look in the node_modules folders all the way up to the root of the filesystem and thus will not find the modules specified by paths in
tsconfig
. -- introduced in tsconfig-paths readme.
In conclusion, directly require tsconfig-paths
into dependencies list and load tsconfig file on run could be a quick solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With