22

These package names are pretty confusing, they seem like they do the same thing yet 'fetch' looks to be abandoned yet not marked as deprecated (last commit 3 years ago). Judging from the download counts people are probably downloading 'fetch' when they should get the supported and maintained 'node-fetch' package.

If you're building a Reactjs app, is fetch already built in? If so, is it different than 'node-fetch'?

What is the suggested package npm users should use?

https://www.npmjs.com/package/fetch

https://www.npmjs.com/package/node-fetch

7
  • 2
    React applications are sometime run on the browser, which, in most cases, has fetch built in. However, sometime they are run in the context of Node.js, a JavaScript environment without the built-in methods of a browser, which does not have fetch built in. Whether you need it or not is up to your specific circumstances. Stack Overflow is not the venue for "cleaning up" npm. Jun 1, 2020 at 15:57
  • 1
    I’m voting to close this question because Stack Overflow is not a venue for gathering help to clean up external repositories. Jun 1, 2020 at 15:59
  • I think it's a valid question @HereticMonkey. He's asking for the difference between the libraries and to clear up confusion about their intended uses. They are not asking to clean up the code.
    – 7wp
    Jan 10, 2023 at 15:02
  • You may wish to compare the dates of comments made and those of the last edit, comparing the contents. Previously, it did ask to clean up the code. It is now completely off-topic as it asks for a recommendation for a library, which has been off-topic for many years. See help center Jan 10, 2023 at 22:00
  • 1
    The information provided in the accepted answer is the internet gold we need, and the metadata of SO is the only way to ensure it reaches an audience. I don't want to argue the fine points of SO terms of service, but you are throwing the baby out with the bathwater if you keep going after questions and answers like this IMHO, @HereticMonkey.
    – moodboom
    Apr 2, 2023 at 19:18

2 Answers 2

31

A bit of history

Fetch is a standard created in 2015 by the Web Hypertext Application Technology Working Group (WHATWG). It was meant to replace the old and cumbersome XMLHttpRequest as means for issuing web requests. As it was meant to replace XMLHttpRequest the standard was clearly targeted at browsers rather than Node runtime, however due to it's wide adoption and for cross compatibility reasons, it was decided that it should also be implemented in Node.

Nonetheless, it took Node team roughly 3 years to implement experimental fetch in Node v16. Although still experimental it is now enabled by default in Node v18.

Because it took Node dev team so long to implement the Fetch standard, the community took matter in their own hands and created the node-fetch package which implements the Fetch standard.

The fetch package that you have mentioned is just coincidentally named the same as the standard but it has nothing to do with it other than that they both aim to "fetch"/"request" resources from the web.

What should you use?

In the past browsers used XMLHttpRequest API and Node used its own http.request. We now have the opportunity to bring those two ecosystems closer still by having them both use the Fetch API. This increases code interoperability and even allows code sharing between the browser and Node in certain cases.

Now, there are other popular packages out there such as axios or requests that still don't use Fetch under the hood but rather continue using Node's http library. Not using Fetch reduces inter-compatibility and therefore I don't think you should keep using either of them unless they convert, which is unlikely in the near future.

Instead, you should consider using Node's native fetch or node-fetch package . Which one though? Well, my opinion is that the Node's fetch is still in early phases but given it has the support from the core Node team I would bet on that. I suppose node-fetch has a wider adoption of the Fetch standard but I think over time it will become redundant as the Node's native fetch becomes fully implemented.

Update 22 March 2024

As of Node 21, Node's Fetch API is marked as stable.

Please see the following answer by 'Daniel Viglione' for more detailed comparison between node fetch API and node-fetch package

3
  • 1
    @7wp Because the answer was given two years after the question was asked. Also, the question asker is the only one who can accept answers and is well within their rights to not accept any answer. Jan 10, 2023 at 22:03
  • 2
    Thank you, this is the good stuff that is near-impossible to discover searching the internet which is just riddled with outdated invalid information.
    – moodboom
    Apr 2, 2023 at 18:53
  • I would add node-fetch has support for "agent" so you can use it with a proxy for instance
    – Poyoman
    Sep 14, 2023 at 15:28
0

Both does the same thing, only difference what i see is node-fetch is compatible API on Node.js runtime, fetch is more specific to browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.