A
A
alex4answ2020-11-10 20:44:44
typescript
alex4answ, 2020-11-10 20:44:44

How to use tsconfig path aliases?

Good afternoon, why are aliases not replaced on the way to modules during compilation?
tsconfig:

"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
  "@api/*": ["api/*"]
},

import routes from '@api/routes';
VS Code sees the file, on hover it shows the full path to it. pjroot/src/api/routes.ts

But when I run ts-node or when compiling and then running it, I get an error: And in compiled files, aliases remain aliases, they are not replaced by real paths, can I somehow configure this? Otherwise, why do we need aliases in ts at all? I would not want to use third-party modules such as moduleAliases and so on for such a simple task. Most likely, this task is solved by the compiler, but it was not possible to google and find a solution, what am I doing wrong?
Error: Cannot find module '@api/routes'


Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-11-10
@alex4answ

No, there is no out-of-the-box solution. And it won't. Why? Fuck you, that's why .
There are a bunch of third-party crutches, google it to your liking.
PS

Otherwise, why do we need aliases in ts at all?

In fact, the feature was pathswritten down to reflect the behavior of fashion bundlers, such as webpack, and not vice versa. Those. it is supposed to be pathsused only when there is already a assembler that implements all the logic, and not as the primary configuration source.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question