Issue with path aliases not working when using imports (TypeScript)
Not sure what exactly is going on, normally I'm only using TS paths like
"@lib/*": ["src/lib/*"]
but I wanted to start implementing something similar in my bot to make it so I don't have a bunch of ../../../
and whatnot when I'm having to import certain types/classes/etc..
Never had any issues with this while using NextJS, but I assume this has to be an issue with my tsconfig/package.json files. This is the error I'm getting at the moment:
Currently this is my tsconfig.json:
If needed I can share my package.json as well. Just not sure what I'm what I'm doing wrong here... This is a real pain because I just started using eslint more to improve my code and so I'm doing a lot of refactoring and.. yeah. Considering my codebase it's a bit overwhelming. I'd like to figure out how to get these ts paths working though if possible.Solution:Jump to solution
The error is a NodeJS runtime error. Not a TS compile error. TS doesn't recalculate aliases and output resolved paths. You need to also configure your aliases in package.json subpath imports. Note that you also need to switch to # instead of @ then because that's what node requires. https://nodejs.org/api/packages.html#subpath-imports
You can look at various examples from /tag bots ( @Spinel command)...
8 Replies
Solution
The error is a NodeJS runtime error. Not a TS compile error. TS doesn't recalculate aliases and output resolved paths. You need to also configure your aliases in package.json subpath imports. Note that you also need to switch to # instead of @ then because that's what node requires. https://nodejs.org/api/packages.html#subpath-imports
You can look at various examples from /tag bots ( @Spinel command)
Recommend you also review the options set by the extended configs btw. You have some duplicate config options. Just go into your node_modules to compare.
ah okay gotcha
it actually does allow me to import like that in some files, just not in others
although this is probably working because the file is one just exporting interfaces
also not sure if this is relevant, but i am using a tsconfig.eslint.json file as well with the following in it:
but I have modified my tsconfig.json slightly with the following in it:
I'm sorry but I'm not sure how this is relevant. I already gave you the solution. You need to provide the aliases for NodeJS as well. Exactly as I described.
Yeah my apologies, I was tired and wasn't quite thorough in going over everything you said. I really appreciate the help in better understanding this so thank you
@Favna by the way I also just wanna say... Thank you for all the effort and everything you've done and everyone else has done with this framework and everything associated with it. I've loved working with it and it's made developing my bot a very enjoyable and rewarding project. As a Discord bot developer that uses TypeScript, I greatly appreciate all the help you've given answering this question and the ones I've had in the past.