Trying to set-up a Node.js application in Visual Studio Code using TypeScript, but ran into an issue with importing modules.
Started off with an index.ts with an import -> import config from './app/config.json';
this returned an issue (import statement outside a module) I resolved by adding 'type': 'module'
to the package.json
file.
this returned an issue (unknown file extension .ts) I am supposed to resolve by removing the same 'type': 'module'
I just added, or by using the ts-node dependancy.
I've instead tried to find a way around using the dependancy by using only require-statements in my index.js file and removing the 'type': 'module'
statement again. This essentially leaves me with two options;
- Either remove all import-statements and only use require-statements. However, TS keeps poppig up telling me to use import-statements instead. This feels very counter-intuitive because the import-statements are what breaks the application.
- Or use the ts-node dependancy, something I feel reluctant towards as a 'solution' -> if this is truely the only way to use import-statements and TS at the same time (something TS seems to heavily favor in the first place) why is it not part of TS in the first place?
I feel like I am missing something, but right now everything I try is leading me in circles; TS seems to imply using import over require is favored, but using import-statements requires me to define 'type': 'module'
, which then breaks the application because it doesn't recognize .ts-files anymore. ts-node seems to be the only solution, but why isn't it included into TS if it is?