#Need Help with TypeScript + Node.js Backend (ESM + ts-node)
76 messages · Page 1 of 1 (latest)
i would recommend not using ts-node if you aren't familiar with the typical ts set up already
What would you recommend instead?
tbh i'd recommend just using tsc + node
Do I need anything apart from that?
though, recently node is getting some capability to run ts directly with strip-types
if you do want a ts runner, tsx seems to be a modern version of ts-node
you don't need anything else, no
What a ts runner allows is that it is faster to develop because you don't have to generate a build everytime you make a change right?
basically, yes
So tsx maybe a nice add to my project
sure
in that case, i'd recommend understanding the basic tsc+node first, then adding tsx on to that for ease of use
Well I need to learn and do at the same time lol
Now I also added two ts configurations: allowtsextensions and addrelativeimports otherwise you have to use .js extensions with .ts files.
Learning curve isn't that complicated with tsx opposed to ts-node. Would you say?
can't say, i don't use either
.js extensions with .ts files.
this is the correct use in the case of building to js with tsc, btw
tsx largely is a drop-in replacement for ts-node.
But now you can enable rewriterelativeimportextensions and allowimporttsextensions
And that allows for using .ts in the imports
What do you think? Isn't it better?
I would just use .js extensions
ah, that's landed now?
Yeah a few days ago
i'd personally still use .js probably
Even in work projects you only use tsc?
i don't work
We use tsc as part of our build step and run the JS code at my work, yeah. Probably it'd be fine to have something like tsx running on our production instances, but it's just one less variable to have to deal with.
But isn't tsx something used for running ts in development instances?
tsx is just something that compiles ts and immediately runs it
how you use it is up to you
So with tsx I can quit tsc for production
please don't advertise your dms.
Just kicked them if that's going to be how they join the server
First message AFAICT.
yeah seems like it
?
I don't personally recommend it.
Why not?
Probably it'd be fine to have something like tsx running on our production instances, but it's just one less variable to have to deal with.
Got it
also with tsx you'd be lazy transpiling, wouldn't you
Ok then I will rewrite imports from .ts to .js, remove ts-node and add tsx (for ease of prototyping)
Feels better?
One thing I noticed with imports is that I can't no longer use barrels and now you have to reference it by index.js.
Yeah, ESM doesn't support importing directories
yeah that's a feature of cjs that esm doesn't have
What's the alternative?
You could compile your code down to CJS - which is the older, node-specific module format; but I probably wouldn't recommend it.
https://nodejs.org/api/esm.html is the documentation on ESM specifics in node
But I mean is there a workaround to barrels?
Importing the index file, like you said.
Or just do straight imports instead?
The old version was just a shortcut for requiring the index file.
I mean technically the idea of the index file is to do a barrel. Then I'd rather remove it and do straight imports.
Sure? I don't personally like barrels or recommend them.
But I'm not really sure why it's related to the change from "import fooBarrel from "./foo" to import fooBarrel from "./foo/index.js"
I didn't understand what you just said
What I meant is that I'd rather remove barrels from my code since they can't be used. So I would remove index files
Barrel files can still be used, they're just imported differently.
A file at foo/index.js that imports and exports the contents of the foo directory is still a barrel file even if you have to explicitly import it as foo/index.js
"Barrel files" is really just a specific convention of organizing code, not a language feature.
The only actual runtime change is that CJS allows you to omit "/index.js" from import paths and ESM does not.
Yeah but maybe the nice thing was to omit the /index in the imports
Do you think tsconfig bases node23 is a good starter?
If you're using node 23, yeah.
I'm having issues importing a json file https://stackoverflow.com/questions/70106880/err-import-assertion-type-missing-for-import-of-json-file
And with and assert are only used when module is set to esnext node18 nodenext
Thoughts?
I did a few chatgpt queries and resolve it. Thank you anyways
Wow! Using tsx and it was immediately working
not my job lol