Using tsc-alias
package we are able to make use of alias paths in tsconfig.json
. This way, modules written in the src
directory can easily be referred to by @/path/to/file
rather than possible ../../../../path/to/file
.
A minor refactoring to dotenv config was also made to make use of the one-liner as per their documentation
Pull Request type
Please check the type of change your PR introduces:
- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [x] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):
What is the current behavior?
Currently now, if you want to use an alias path, which is standard functionality in tsconfig.json
, you will be met with an error message a la Error: Cannot find module '@/routes/router'
.
What is the new behavior?
You are now able to add as many alias' in tsconfig.json
as you please. As an example:
{
"compilerOptions": {
...
"paths": {
"*": ["node_modules/*", "src/types/*"],
"@/*": ["./src/*"],
"@routes/*": ["./src/routes/*"],
"@database/*": ["./src/database/*"],
"@models/*": ["./src/models/*"],
...
}
},
"include": ["src/**/*"]
}
Does this introduce a breaking change?
Other information
The import 'dotenv/config'
is just a simpler and more elegant implementation of the already provided solution. So just syntactic sugar really.