#Need Help with TypeScript + Node.js Backend (ESM + ts-node)

76 messages · Page 1 of 1 (latest)

mellow orchid
#

Hey everyone, I'm trying to set up a TypeScript Node.js backend using ESM and ts-node, but I'm struggling to get the right configurations.

I’ve seen some templates, but many suggest using a bundler (which I believe is more for frontend). Does anyone have a base template or recommended settings for this setup?

plain mountain
#

i would recommend not using ts-node if you aren't familiar with the typical ts set up already

mellow orchid
#

What would you recommend instead?

plain mountain
#

tbh i'd recommend just using tsc + node

mellow orchid
#

Do I need anything apart from that?

plain mountain
#

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

plain mountain
mellow orchid
#

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?

plain mountain
#

basically, yes

mellow orchid
#

So tsx maybe a nice add to my project

plain mountain
#

sure

#

in that case, i'd recommend understanding the basic tsc+node first, then adding tsx on to that for ease of use

mellow orchid
#

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?

plain mountain
#

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

delicate notch
#

tsx largely is a drop-in replacement for ts-node.

mellow orchid
#

And that allows for using .ts in the imports

#

What do you think? Isn't it better?

delicate notch
#

I would just use .js extensions

plain mountain
#

ah, that's landed now?

mellow orchid
#

Yeah a few days ago

plain mountain
#

i'd personally still use .js probably

mellow orchid
#

Even in work projects you only use tsc?

plain mountain
#

i don't work

delicate notch
#

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.

mellow orchid
#

But isn't tsx something used for running ts in development instances?

plain mountain
#

tsx is just something that compiles ts and immediately runs it
how you use it is up to you

mellow orchid
#

So with tsx I can quit tsc for production

plain mountain
#

please don't advertise your dms.

delicate notch
#

Just kicked them if that's going to be how they join the server

plain mountain
#

uh, they didn't seem to be new

#

but eh shrugsinjapanese not my job lol

delicate notch
#

First message AFAICT.

plain mountain
#

yeah seems like it

mellow orchid
delicate notch
#

I don't personally recommend it.

mellow orchid
#

Why not?

delicate notch
#

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.

mellow orchid
#

Got it

delicate notch
#

The tsx docs are non-committal: "up to you and your risk tolerances"

plain mountain
#

also with tsx you'd be lazy transpiling, wouldn't you

mellow orchid
#

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.

delicate notch
#

Yeah, ESM doesn't support importing directories

plain mountain
#

yeah that's a feature of cjs that esm doesn't have

mellow orchid
#

What's the alternative?

delicate notch
#

You could compile your code down to CJS - which is the older, node-specific module format; but I probably wouldn't recommend it.

mellow orchid
#

But I mean is there a workaround to barrels?

delicate notch
#

Importing the index file, like you said.

mellow orchid
#

Or just do straight imports instead?

delicate notch
#

The old version was just a shortcut for requiring the index file.

mellow orchid
#

I mean technically the idea of the index file is to do a barrel. Then I'd rather remove it and do straight imports.

delicate notch
#

Sure? I don't personally like barrels or recommend them.

mellow orchid
#

I don't do them also

#

But I'm migrating legacy code

#

I mean old js code

delicate notch
#

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"

mellow orchid
#

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

delicate notch
#

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.

mellow orchid
#

Yeah but maybe the nice thing was to omit the /index in the imports

#

Do you think tsconfig bases node23 is a good starter?

delicate notch
#

If you're using node 23, yeah.

mellow orchid
#

Thoughts?

mellow orchid
#

I did a few chatgpt queries and resolve it. Thank you anyways

mellow orchid
#

Wow! Using tsx and it was immediately working