#TypeScript Compilation Error

39 messages · Page 1 of 1 (latest)

thorny fern
#

Trying to setup TS for es6 module. I'm using node version v20.12.1. I've tried switching to different node versions.

I'm just using 1file 1 line, console.log() for initial setup.

Any help is appreciated, many thanks!

dire cipher
#

Do you not know how to use import?

olive turtle
#

!screenshot

digital nightBOT
#
`!screenshot`:

Rather than screenshots, please provide either code formatted as:

```ts
// code here
```
Or even better, as an example on the TypeScript playground that is as simple as possible and reproduces the issue. This makes it easier to help you and increases the chances of getting an answer.

olive turtle
#

but we don't really need all of that, showing the part that's actually giving errors would help

thorny fern
thorny fern
olive turtle
#

sounds like that's not a valid module then?

#

could try adding a dummy export {}

thorny fern
#

Still the same error.
I read couple of article that thr node version can be issue, but I've switched to latest one, the prev i was using was v15.
Still the same err for both

jovial niche
#

what does D:\test\app.ts look like? (and as previously suggested: code as text is better than a screenshot)

thorny fern
thorny fern
jovial niche
#

i don't think i've used that package. you could try compiling with tsc and then running via node to see if you get the same issue or not. i know tsx is also popular these days

thorny fern
#

Ah, many thanks for the article, issue looks the exact same....

sand thistle
#

while ts-node is convenient, I'd recommend not to use it for beginners, since it requires quite a bit of prior knowledge about module systems in JS

#

you need to pass --esm when calling ts-node

#

you can see in the logs the ts-node loader is cjs, which won't work with es modules

#

also express is a cjs package, so you will need esModuleInterop, but I see you already have it enabled, so that's good

#

@thorny fern

thorny fern
#

Thanks for the suggestion....I'm self learning so need to start from somewhere right🙃...
I'll checkout the given link, thanks!

sand thistle
#

also you should have "type": "module" in your package.json

#

for nodejs to start in esm mode

#

but tbh, I wouldn't go down that road

#

👉 it's way easier to just use cjs when getting started @thorny fern

#

especially if you don't have any specific reason to use esm

thorny fern
sand thistle
#

so need to setup ts on es module to setup ts on the main express code
wdym?

thorny fern
#

I mean I've given type: module as coz I've written express code on es6.
So I'm not checking out TS in cjs

sand thistle
#

basically

#

TS code will always use ES modules, by design, it's your only "option"

#

but when you compile your TS code into JS,
you can chose what module system to use to run the code

#

now, express uses CJS at runtime

#

so your emitted JS code should use CJS too

thorny fern
#

Ah i see i see....
But considering that my express is in es6....and if I've type:module defined in package.json the Tas isn't working

#

I'll prolly check the urls shared above and get better grasp