DEV Community

Cover image for The NodeJS 18 Fetch API

The NodeJS 18 Fetch API

Andrew Baisden on May 24, 2022

NodeJS 18 introduced some new cool features and one of the most useful ones is the built in Fetch API. What this means is that we longer need to us...
Collapse
 
lexlohr profile image
Alex Lohr

For libraries, I found a rather nice pattern to allow users to only install node-fetch if they need it:

// package.json
{
  "peerDependencies": {
    "node-fetch": "*"
  },
  "peerDependenciesMeta": {
    "node-fetch": {
      "optional": true
    }
  }
}

// Run before you use fetch:
if (!globalThis.fetch) {
  Object.assign(globalThis, {
    fetch: (...args: any[]) => {
      try {
        const fetch = require("node-fetch");
        Object.assign(globalThis, { fetch });
        return fetch(...args);
      } catch (e) {
        console.warn(
          '"\x1b[33m⚠️ package missing to run fetch on the server.\n Please run:\x1b[0m\n\nnpm i node-fetch\n"'
        );
        Object.assign(globalThis, { fetch: () => Promise.reject() });
        return Promise.reject();
      }
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

This way, users get a helpful message if they forget to install node-fetch and you can avoid superfluous dependencies.

Collapse
 
suchintan profile image
SUCHINTAN DAS

Thanks Andrew πŸ‘¨, for sharing this information.

Can't wait to try it out on the system πŸ‘.

One suggestion πŸ’­. Can you explain the changes that are being introduced on the new version of fetch api compared to the previous version ✍.

I think it would make the blog a full packed information about the topic and would also increase the understanding of people who can't have time to look on the documentation to know about the information β„Ή.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Hmm the official documentation is probably the best place to learn about it developer.mozilla.org/en-US/docs/W....

I believe that the NodeJS implemented of the fetch API is the same as the one we already use in the browser.

Collapse
 
suchintan profile image
SUCHINTAN DAS

Thanks Andrew πŸ‘,

Let me go through the documentation to know what amazing stuffs we have on this new version of Node JS 🀠

Collapse
 
judis07 profile image
Arvind M

Clean and Concise.πŸ‘πŸ‘πŸ‘

Collapse
 
andrewbaisden profile image
Andrew Baisden

Thanks much appreciated.

Collapse
 
dinerdas profile image
Diner Das

Glad I came across this.

Collapse
 
vareksun profile image
Varek

Very detailed, thanks.

Collapse
 
snelson1 profile image
Sophia Nelson

Well described post

Collapse
 
surajondev profile image
Suraj Vishwakarma

NiceπŸ”₯, Now we don't have to depend on any other library for fetching data. Btw great article , Andrew.

Collapse
 
strivecode profile image
Abdul-Razak

Wawu that was timely. You have saved me a bunch of time. Thank you!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Sure no problem.

Collapse
 
spock123 profile image
Lars Rye Jeppesen

Great article but it would have been nice if you also showed POST requests, not entirely simple GET

Collapse
 
upsylondev profile image
UPSYLON DEVELOPPEMENT

Hello. cool stuff & article
but how manage headers with this api ?