Ir al contenido principal

Entradas

Mostrando entradas de febrero, 2021

Using paths in tsconfig.json

How frustrating can be managing a big Typescript project in which all the module paths are relative to the importing file, as the next example. Every time we move a file, and therefore its relative path changes regarding its imported modules, we must sacrifice time and patience fixing a train of dots and slashes that too many times are fixed by trial and error. import {Request} from "express"; import {foo} from "../../../../types"; import {bar, baz} from "../../../../../myModules/bax"; Hopefully Typescript's tsconfig.json file allows specifying path aliases by setting the paths option. This option, together with the use of the npm package module-alias allows converting the previous mess into a piece of fine art: import {Request} from "express"; import {foo} from "@types"; import {bar, baz} from "@myModules/bax"; As said the process implies not just configuring tsconfig.json but also using the package