I am currently utilizing typescript v3.6.4
and have the following snippet in my tsconfig.json
:
"compilerOptions": {
"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
"@config/*": ["config/*"],
"@config": ["config"],
}
}
Additionally, I have set up a module alias in my package.json:
"_moduleAliases": {
"@config": "dist/config"
}
The folder structure of my project looks like this:
src
|-config
|-index.ts
|-app
|index.ts
|logic.ts
|-dist
When I attempt to import a module in app/index.ts like so:
import 'module-alias/register';
import config from '@config';
and run npm start
with the command:
"start": "node -r ts-node/register ./src/app/index.ts",
Although tsc
compiles successfully, npm start
throws an error:
Error: Cannot find module '@config' in src/app/logic.ts
To resolve this issue, I must also include the line:
import 'module-alias/register';
in src/app/logic.ts
It appears that adding import 'module-alias/register'
in every file where aliases are used is necessary. Is there a way to configure this more efficiently?