question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

node-fetch should provide a polyfill entrypoint

See original GitHub issue

This has been an issue that has came back to me several times, but the fact that v3 is ESM-only module makes this a pain point with node-fetch. Read more bellow:

When is this an issue?

  • using a library that expects fetch available as a global
  • working with an isomorphic codebase
  • you want to run a module/some code originally designed for the browser in your node codebase

Why are the existing solutions inappropriate?

The doc suggests doing this:

import fetch from 'node-fetch';

if (!globalThis.fetch) {
  globalThis.fetch = fetch;
}

But this is actually problematic with ESM modules. If you use a library expecting fetch as a global (eg. react-relay-network-modern). There are typically three locations where you would import node-fetch:

  1. right before you import the library (unless you also run that code in the browser)
  2. in your server entry point
  3. in your bundler entry point

In all three cases using ES imports the above code won’t be guaranteed to have assigned fetch on the global object before something is done with it, and so the only way to address that is to use an import file specifically for polyfilling.

Solutions

The correct solution is to provide a polyfill entrypoint that can be used for this purpose:

import 'node-fetch/polyfill';

This can either be implemented on this project or with an external package. An external package would either include node-fetch as a peer dependency, requiring the user to use 2 packages, or worse, it may be using node-fetch as a dependency.

Either way, this is an issue that will keep coming back until a proper solution is documented. Perhaps a middle-ground solution is to link to a module that properly polyfill node-fetch in the documentation.

Prior art and discussion

https://github.com/node-fetch/node-fetch/pull/119 (by yours truly) https://github.com/node-fetch/node-fetch/pull/521

Why does this belongs on this package?

  1. This is a polyfill for node, not for any other environment. I agree with @bitinn that providing a browser.js file was out of scope:

    On https://github.com/node-fetch/node-fetch/pull/537

    Thx for the PR but I believe this is better left for third-party module, where the author knows exactly what they want.

    (we made mistake providing a browser.js and we regret it since, as people use it differently and sometimes in non-nodejs environment like react-native.)

  2. There is pretty much only one way to do it, https://github.com/node-fetch/node-fetch/pull/119 and https://github.com/node-fetch/node-fetch/pull/521 both use the same approach.

Why is it an even bigger issue today than 5 years ago

  • node-fetch v3 being an ESM-only module, it will often require creating a separate file to properly polyfill it
  • browser code is now fairly safe to use fetch without any client polyfills and so it can be more frequently expected to rely on a global fetch

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
OnkelTemcommented, Dec 19, 2021

Any updates on this?

0reactions
jimmywartingcommented, Jan 16, 2022

Any updates on this?

wondering that as well, a bit on the fence here… if it ain’t necessary then maybe we can leave it as is… it’s harder to revert something that got released out to the open…

“If it ain’t broke, don’t fix it”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Babel not Polyfilling Fetch when using babel-preset-env
Babel doesn't add polyfill for web specifications. You can only use polyfills to implement ECMAScript's proposals listed here.
Read more >
whatwg-fetch - npm
The fetch() function is a Promise-based mechanism for programmatically making web requests in the browser. This project is a polyfill that ...
Read more >
babel/polyfill
To include the polyfill you need to require it at the top of the entry point to your application. Make sure it is...
Read more >
API Docs | fetch-mock - Wheresrhys
It supports most JavaScript environments, including Node.js, web workers, service workers, and any browser that either supports fetch natively or that can have...
Read more >
Using the fetch API inside Node.js - Christoph Zauner's Blog
When initializing the server you have to make the fetch function globally available as outlined below: <...> // enable node-fetch polyfill for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found