r/node • • Feb 01 '22

Fetch API has landed into Node.js

https://github.com/nodejs/node/commit/6ec225392675c92b102d3caad02ee3a157c9d1b7
280 Upvotes

31 comments sorted by

62

u/Zipdox Feb 01 '22

Now we can finally ditch node-fetch, which I've wanted to do ever since it switched to ESM.

13

u/Solonotix Feb 01 '22

Isn't ESM the "way forward" for Node.js? I've seen tons of posts a out how TypeScript allows use of modules, and that's always seen as a good thing, but then discussion of ESM always seems to be met with disdain.

26

u/codyebberson Feb 01 '22

Yes, ESM is the way forward. Server-side ESM is still a bit clunky. The gentle migration path is to dual build CommonJS + ESM. The node-fetch team took a hard stance of ESM-only, which breaks a lot of projects.

18

u/Guisseppi Feb 01 '22

Node’s module management has been a clusterfuck and I get that there’s history behind it but damn how long have we have ESM in feature flags only for a half baked release, I hate the semantics of using .mjs or adding type module, and if all the planets align then node will read it as an external module

4

u/Zipdox Feb 01 '22

I don't care if it's "the way forward". My program uses CommonJS and now it's broken because it suddenly changed.

6

u/MatthewMob Feb 01 '22

Moduloze or 5to6.

Also your problem will only get worse as time goes on, it's worth it to take the pain to upgrade now to save yourself tomorrow.

0

u/[deleted] Feb 01 '22

[deleted]

2

u/spazz_monkey Feb 01 '22

Can you care to explain how to fix it with node and typescript. I get told I'm importing it wrong, but it's the exact same way importing everything else.

4

u/Bloodsucker_ Feb 01 '22

Yeah, what's the problem with ESM? That's the way to go from now on, people. Just do it. If you use typescript you don't even have to do anything.

9

u/profmonocle Feb 02 '22 edited Feb 02 '22

If you use typescript you don't even have to do anything.

I've converted a few medium-sized Typescript projects from CJS to ESM.

Unfortunately, TypeScript still has some issues with Node & ESM. Notably, many popular CommonJS packages don't work with named imports such as import { foo } from 'bar'. You have to do import bar from 'bar' and then use it like bar.foo() or destructure it like const { foo } = bar. However, typescript won't tell you this at all. The broken import will not only compile fine, TypeScript will actually suggest it via auto-imports.

Also, modules in ESM require the .js extension - import foo from './bar' is invalid. But even if your project uses ESM, TypeScript's auto-imports will default to not using an extension. Again, it suggests and compiles code that immediately fails at runtime.

The second issue is apparently going to be improved in an upcoming release, but there's no solution in sight for the first issue.

Neither of these are the end of the world, but it means that converting a CommonJS codebase written in TS isn't as simple as changing a couple config flags, and the development experience post-conversion is noticeably more frustrating. And unless you want to use top-level await, you don't actually gain anything in exchange for this frustration. Except for the fact that some popular modules are dropping CJS support.

ESM may be the way forward, but at the moment it feels like a step backwards in terms of dev experience.

13

u/matthewsilas Feb 01 '22

Any app built before esm can’t justify the expense to refactor to esm.

3

u/Solonotix Feb 01 '22 edited Feb 01 '22

This was exactly the same mindset for Python2 to Python3. Honestly, it's a lot less painful if people just adopt the new standard, rather than continuing to borrow against the technical debt of staying on a platform that has officially been identified discussed as a potential target for deprecation (maybe).

Edit: clarified it is not official. The core Node team has not made an official statement as of this date.

6

u/matthewsilas Feb 01 '22

source? I’d prefer not to start rumors. AFAIK cjs is not targeted for deprecation.

1

u/Solonotix Feb 01 '22

Good call, it isn't official. Discussion can be found here. It's a hot topic, and there's a lot of mentions of libraries or frameworks ditching support for CJS, but Node.js has been neutral on the topic so far.

2

u/gigastack Feb 02 '22

There's lots of blame to go around but I've been dealing with this pain too.

1

u/spazz_monkey Feb 01 '22

I don't even know how to get node fetch to work with express and typescript. I constantly get the error, had to go back to V2 or whatever it is that works.

2

u/Zipdox Feb 01 '22

Yup, that's why I hate it. The dev said he would maintain V2 with critical fixes but NPM says V2 has high severity vulnerabilities.

14

u/leeharris100 Feb 01 '22

I've been using got for so long that this doesn't really affect us at all

That being said, always nice to have a better standard library

3

u/cardyet Feb 02 '22

What made you preference Got over others?

2

u/leeharris100 Feb 02 '22

It's just so clean and simple for 99% of use cases but has the power there for more advanced stuff.

We actually use Nest for all of our new microservices and it has its own HTTP library. It is a wrapper around Axios that turns the response into an observable.

Personally I love observables and would prefer working with them most of the time, but it's conceptually MUCH harder for your average dev to understand compared to async/await.

While on the Nest HTTP page there was a header that said, "you can also use any http library like got or undici." Tried out got and loved it!

3

u/emcell Feb 01 '22

Thanks. That looks promising.

1

u/duckducklo May 02 '22

Do you use any of the advanced stuff got has that axios doesn't? Or if you don't know about axios, fetch doesn't?

5

u/[deleted] Feb 02 '22

Stop trying to make fetch happen, Gretchen

1

u/BlazingFire007 Feb 04 '22

When can I expect this update to release? Super hyped for this!

1

u/jetanthony Feb 19 '22

So what version will this be released in?

1

u/Dreadweave Feb 24 '22

I just installed v17.6.0 but its not working. How do i get it?