Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using paths in TypeScript in a NodeJS project

I'm trying to use the paths property in tsconfig.json in a NodeJS project.

In tsconfig.json I have something like this:

"baseUrl": ".",
"paths": {
  "@myApp/server/*": [
    "server/src/*"
  ],
  "@myApp/common/*": [
    "common/src/*"
  ]
},

Running tsc outputs all JS files as expected, but they retain the @myApp... imports. As a result, node won't run as it can't resolve all modules having as path @myApp....

I can't find a way to convert the paths I've set in tsconfig.json to a value that can be used by node. I've only found this question on SO, but it's quite outdated and it does not lead to a clean solution.

Do we have a way to transpile TS to JS in a way in which we are able to use paths?

like image 528
don Avatar asked Aug 04 '18 08:08

don


1 Answers

If anyone out here is using with nodejs and tsconfig-paths. You can use the following commands to make the absolute paths work:

// With ts-node    
ts-node -r tsconfig-paths/register src/main.js

// With node    
node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/main.js

Got this from here: https://github.com/dividab/tsconfig-paths/issues/61

like image 140
FacePalm Avatar answered Sep 20 '22 19:09

FacePalm