2

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?

Sonav
  • 19
  • 1
  • 4
  • ts-node provides ready to use setup that doesn't require to run a compiler separately. It'a unknown what's your setup but most likely is that tsconfig is wrong – Estus Flask Feb 23 '22 at 12:55
  • @EstusFlask if it's relevant I can post the contents of my `tsconfig` - as far as I can tell the (most) relevant parts like `esModuleInterop: True` and `module: commonjs` are present. – Sonav Feb 23 '22 at 13:07
  • Also consider explaining how you compile/run the app, it's relevant. It's unclear what exactly happens with index.ts. – Estus Flask Feb 23 '22 at 14:13
  • simply by running `node index.ts` - or `ts-node index.ts` rather, as I've installed and am now using ts-node. It solved my issues as promised in the SO-thread I mentioned, but I'm still confused as to why it's a seperate dependancy from TS ? – Sonav Feb 23 '22 at 20:36
  • 1
    The errors you've got are only the tip of the iceberg. You can't run TS files with `node index.ts`. Node runs JS scripts. TS is not valid JS (technically it can be as it's a superset of JS, but IRL it never is). This requires to compile .ts files to .js files with TS, then you can run them as usual. This is what ts-node is for, to skip the hassle with a compiler. "why isn't it included into TS if it is?" - TS is a compiler, it's clueless of a platform where the code runs, a browser that runs TS natively isn't included as well. – Estus Flask Feb 23 '22 at 20:49

1 Answers1

0

Im learning TS right now too and I find the same issue you have. The problem with import is that you need to declare your project as a

'type': 'module'

in order to use this feature, but it seems that ts-node throwing an error because it cant convert it, well how i solved the problem to import an json was doing an step backwards and deleting the 'type': 'module' from my package.json, then running:

npm i --save-dev @types/node

and requiring your json in the ts file as follow (ex):

const pkg = require('./package.json');

Dharman
  • 30,962
  • 25
  • 85
  • 135
devperate
  • 51
  • 2
  • require is indesirable in ts because it's type unsafe. There's obsolete import..require syntax but it will naturally have the same problem because this isn't valid JS – Estus Flask Feb 23 '22 at 22:53