I am using typescript v3.6.4 with the following tsconfig.json snippet:
"compilerOptions": {
"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
"@config/*": ["config/*"],
"@config": ["config"],
}
}
and module alias in package.json:
"_moduleAliases": {
"@config": "dist/config"
}
I have the following folder structure:
src
|-config
|-index.ts
|-app
|index.ts
|logic.ts
|-dist
Now in app/index.ts, if I do:
import 'module-alias/register';
import config from '@config';
and my npm start commands is:
"start": "node -r ts-node/register ./src/app/index.ts",
tsc will compile successfully but npm start will give error:
Error: Cannot find module '@config' in src/app/logic.ts
And the only way to fix this is to also add
import 'module-alias/register';
in src/app/logic.ts
Seems I have to add the import 'module-alias/register' in every file that I do alias? Is it a way to configure this?
module-alias/registeronly once in youapp/index.tsit should load all other imports in e.g. inapp/logic.tswithout the need to import it there again. That's also what their docs say.tsconfig.ts). That's why you need to loadmodule-alias/registerand of course in order to make it work you have to at least import in once, otherwise the packages code would not be included.