Node.JS Revolution: Farewell to Axios and Fetch API in Version 17.5.0
Tags: node, javascript, beginners, tutorial
Published: 2022-04-06 17:00:54

Every self-respecting student of programming has already needed to use libraries (the famous “lib”) in the development of their applications. Examples include the Axios and Fetch libs, which are widely used on the front-end and back-end to consume external APIs.

Recently, Node.JS v17.5 underwent two changes that divided opinions, so let's explain how these changes impact the issues of effectiveness and agility in code production. Additionally, I will also discuss the pros and cons of this update.

> Originally published on [Keyholesoftware.com](https://keyholesoftware.com/2022/03/28/node-js-revolution-farewell-to-axios-and-fetch-api-in-version-17-5-0/) by Bernardo Leite on 3/28/22.

<h2>Understanding the Different Libraries</h2>
To start, let's remember how the <b>Axios</b>, <b>Fetch</b>, and <b>Node Fetch</b> libs work.

With an excellent reputation in the community, Axios is a popular Javascript lib for making HTTP requests from Node.JS or <code>XMLHttpRequests</code> through the browser. It also supports the Promise API, which is native to JS ES6, transforms data into JSON, and transforms and intercepts request and response data (request/response). In addition to all this, on the client-side, it supports XSRF protection.

In the Javascript language, Axios is an alternative to the <code>fetch()</code> method because it can do an automatic analysis of JSON and works very well in partnership with Express. While Axios sends the web request, Express responds to these requests.

On the other hand, Axios is an external package that you need to install in your project, unlike Fetch, which is already built into the browser and, therefore, lighter than Axios.

For those who are on the Node-Fetch team, you know that it will be easy and light to install the module. With a one simple <code>npm install node-fetch</code> you have the Fetch resources in a much more direct way and without having to implement the <code>XMLHttpRequest</code>. That is, it is an attraction for those who like a cleaner and more organized code.

Here’s an example of Node-Fetch using <code>async</code> and <code>await</code> asynchronous functions.

```
const express = require(“express”);
const fetch = require(“node-fetch”);
const app = express();

app.get(‘/’, async function(req, res){
const response = await fetch(‘https://dog.ceo/api/breeds/list/all’)
	const app = await response.json()
	console.log(app)
})
app.listen(3000);
module.export = app
```

Note that in the application, you may need frameworks and maybe other libraries in addition to Node-Fetch. This can make the application heavy with so many dependencies.

Simply put, new to the Node.JS ecosystem is a native way to implement the Fetch API without lib or installing external modules.

<img class="alignnone size-full wp-image-26990" src="https://keyholesoftware.com/wp-content/uploads/Bernardo-1.png" alt="Axios and Fetch" width="330" height="97" />

You will now have the <code>fetch</code>, <code>Request</code>, <code>Response</code>, and the <code>Headers</code> as globals. Then, just consume the Fetch API! Here is a very simple example.

<img class="alignnone size-full wp-image-26991" src="https://keyholesoftware.com/wp-content/uploads/Bernardo-2.jpg" alt="" width="512" height="114" />

Now, all you need is to send the request and the external API will respond back in a much simpler way.
<h2>Conclusion</h2>
There are many reasons for you to consider using <code>fetch()</code> in Node.JS because, in addition to being excellent for performing simple requests, we don't need to worry about possible changes in external libs and consequently compromising our applications.

It is still important to remember that native Fetch is not yet available for the LTS version, which means that there may be versioning issues or instability in the latest version. However, it is nothing that compromises the new implementation.

Native Fetch also references <a href="https://undici.nodejs.org/#/" target="_blank" rel="noopener">Undici</a>, which guarantees a significant improvement in latency and file transfer rate.

For more on Node.JS and other popular technologies, take a look at our <a href="https://keyholesoftware.com/blog/">Development Blog</a>. All posts are written by other expert Keyhole consultants.