How to resolve relative path hell (node with typescript)
Asked Answered
L

0

7

I am developing a CLI using node, typescript and other packages. The idea is simple, the CLI gives you an initial structure for your node projects (javascript or typescript)

Well, while testing, I discovered a problem that I have not been able to solve for days. The initial structure is:

├────src
│    ├───api
│    │   └───users
│    │        ├───users.controller.ts/js
│    │        ├───users.interface.ts (just for typescript projects)
│    │        ├───users.routes.ts/js
│    │        └───users.service.ts/js
│    ├───config
│    │   └────environments
│    │        ├───development.ts/js
│    │        ├───index.ts/js
│    │        └───production.ts/js
│    ├───global
│    │   └───services
│    │        └───abstract.service.ts (just for typescript projects)
│    ├───middlewares
│    ├───database.ts/js
│    ├───index.ts/js
│    └───server.ts/js
├───.editorconfig
├───.env
├───.gitignore
├───package.json
├───package-lock.json
└───tsconfig.json (just for typescript projects)

The typescript services files extends of an abstract service (this one is in ./src/global/services/abstract.service.ts). Each component will be in ./src/api directory and have its respective folder (like users component). The relative path to import the abstract class in the users component is ../../global/services/abstract.service

But you can create the component in a different directories, so the previous relative path does not work. To fix this problem I decided to using a routes configuration like webpack. So instead of use the previous relative path, I want to use something like this @global/services/abstract.service

I achieved this if the node project using javascript with babel:

  • @babel/polyfill
  • @babel/cli
  • @babel/core
  • @babel/node
  • @babel/preset-node
  • babel-plugin-module-resolver

But I don not achieved that when is a typescript project. The tsconfig.json has the "paths" options, this works in development, but when I compile typescript to javascript this doesn not work. I tried many ways to resolve that but everything failed. These are the packages that I've used until this now:

  • link-module-alias ()
  • ts-node with tsconfig-path
  • ttypescript
  • @zerollup/ts-transform-paths

The second one works correctly but I must restart for each changes that I make in ts files, maybe I did not configure the scripts correctly in package.json, because the tsc-watch did not work). The links I saw:

Please, if anyone knows how to solve the relative path hell, I will appreciate it if you tell me how, with examples or something to help me

Lots answered 16/9, 2020 at 19:59 Comment(3)
Just a google away... "typescript paths resolver" => github.com/duffman/tspathSesterce
As a workaround to the @Sesterce comment escaping relative path require hell in Node.js and this link as well.Compensation
I chose module-alias for simplicity and effectiveness..Selda

© 2022 - 2024 — McMap. All rights reserved.