Features of Node.js 21: The Newest Version!

Node.js has launched Node JS 21, the most recent version in this line of new releases. They made it available on the 17th of October, 2023. The new update of NodeJS brings several upgrades inside and some features for users too, like adding a reliable fetch API. Also, Node.js 20 has become stable and is now available with long-term support for people who develop software.

Backend developers who know their stuff don't need to be told what Node.js is because of all its great features and how flexible it is. So, if you want to improve your technology tools with Node.js 21, it is possible for you to get the most recent Node.js version on your computer or make an upgrade through easy steps.

Node.js 21; Source: Stackademic

Adding the fetch API is a primary characteristic of Node.js 21. It introduces a uniform method for HTTP requests, making it easier for developers to handle them.

For those who develop in NodeJS, it is beneficial that there is no necessity to depend on outside libraries or intricate coding for the purpose of making HTTP requests and dealing with the answers.

Actually, we will discuss some major internal enhancements that come with Node JS 21 within this article. An important update regarding dependencies is the upgrade from V8 to version 11.8.

New features and updates in the Node.js 21

Node.js 21 features; Source: JavaScript in Plain English

As Node.js develops, it becomes more steady and adds features that are easier to use. Let us now explore the new things found in Node.js 21.

V8 Upgraded to v11.8

A JavaScript engine known as V8 is what drives Node.js. This engine translates and runs the JavaScript code, offering a range of functionalities. Moreover, V8 is utilized by Chromium, which is the open-source initiative that serves as the foundation for Google Chrome and various other web browsers.

The update for Node.js to version 21 includes an upgrade of V8 to version 11.8, which also belongs to Chromium 118. This means that Node.js will work more efficiently and provide new features in the language, like:

Array grouping allows you to collect array elements together using a key function or value. It then gives back an object with keys that match the groups and values that are arrays of the elements in each group.

ArrayBuffer.prototype.Transfer allows you to give the control of an array buffer to a new buffer that has another size. It creates a fresh memory space and creates a new array that holds all the data from the first array. It is no longer possible to use the original array buffer once it is detached.

When you create an error object, it is possible to indicate the reason for the error. By including a cause property, you can attach a personalized message or include the initial error with the new error object. Using this method, you can debug and trace errors in complex applications.

Stable Fetch API Status

Node.js added a new option called --experimental-fetch in version 17.5.0, which made the Fetch API similar to web browsers with features like fetch, header, request, and response globals. Between versions 18.x and 20.x, this feature continued without requiring the flag, although it was still considered experimental.

The Fetch API has become stable in Node.js version 21. Since Fetch is no longer experimental, it can now be used reliably.

const res = await fetch(“https://jsonplaceholder.typicode.com/todos/1“);

if (res.ok) {

const data = await res.json();

console.log(data);

}

Web Streams API Is Stable

To use the Fetch API, you need to work with something called the AbortController interface for stopping fetch requests and also with another thing named the Web Streams API. These were introduced in NodeJs at different times; version 15.0.0 had the AbortController, which became solid in version 15.4.0, while the stable release of the Web Streams API was included later in NodeJs 21.

import { TextDecoderStream } from “node:stream/web”;

async function streamExample() {

const response = await fetch(“https://jsonplaceholder.typicode.com/todos/1“);

const stream = response.body;

const textStream = stream.pipeThrough(new TextDecoderStream());

for await (const chunk of textStream) {

console.log(chunk);

}

}

streamExample();

The older Node.js streams API has not been deprecated or removed. It operates together with the new API, and it is possible to change Web streams back and forth by using the.fromWeb() and.toWeb() methods. These are recent additions to Node 17, but they are still being tested.

llhttp v9.1.2 Strict Mode Enforcement

The update for Node.js llhttp version 9.1.2 now automatically uses strict mode, which lets it use features that were only available in strict mode before.

Several important updates have been made, such as the requirement to insert a new line after headers and sections and stopping data transfer when there is a 'Connection: close' header to improve the following of the protocol.

To retain backward compatibility, use the -insecure-http-parser switch to disable these enhancements.

ES Modules Improvements

In Node.js version 21, there is a new feature with the name experimental-default-type flag. It changes the way that files that are not ES modules or CommonJS get handled by the system. Before this, if you did not say what kind of file it was, Node.js would think it was CommonJS, but now this has changed. If you set experimental-default-type=module, ES modules are supported in the following ways:

Input string provided by -eval or STDIN if -input-type doesn’t specify.

It is relevant for files that finish with.js or have no extension, in case there isn't a package.json file inside the package, or if the nearest parent category doesn't have a type value specified—unless it's within node_modules.

node –experimental-default-type=module index.js

The upgrades make a path for future backing of ES module grammar as standard, which needs just small adjustments.

Module Customization Improvements

Taking away the complex globalPreload hook has made it easier to modify modules by transferring data from application threads to customization hooks and starting communication between different threads.

Performance Improvements

With this release, Node.js performance has improved in the following ways:

To make the Streams API faster, we removed unnecessary checks, used bitmaps, and planned callbacks in a more effective way.

When we open up the responses and break them into smaller sections, it helps to lessen the load on both servers and clients by not having all the information in one big block.

Add flush option to writeFile() functions

Node.js has added a flush choice to the fs.writeFile function group. This option stops old data from appearing when you read next time because it makes sure data gets flushed once writing is done well.

A Built-in WebSocket Client

When you put the experimental-websocket flag in Node.js, it keeps up its promise to offer normal web platform APIs like those made official by WHATWG.

Besides the new API, other available APIs for web platforms are there too, such as Fetch and Web Stream APIs, an AbortController API, a WHATWG URL parser, and Web Crypto APIs.

// index.js

const socket = new WebSocket(“ws://localhost:8080”);

socket.addEventListener(“open”, (event) => {

socket.send(“Hello Server!”);

});

socket.addEventListener(“message”, (event) => {

console.log(“Message from server “, event.data);

});

Although it is not yet suggested for use in production, you have the option to begin trying it out immediately.

node –experimental-websocket index.js

Test Runner Enhancements

In Node.js version 21, they make the built-in test runner better than before. It was already stable in version 20. Now, with this new one, people can use more than one pattern for matching files at the last argument place(s).

Here’s an example:

‘node -test */.test.js */.spec.js’.

The updated function provides more adaptability and management to the final users. It lets them tailor their testing methods according to their project's specific requirements, giving them authority.

Steps to Upgrade to Node.Js 21 Update

Node.Js 21 Update ; Source: LinkedIn

This is the sequence of simple actions you must take to update your system to version 21 of Node.js:

To begin, please go to the official website of Node.js and download the installer for Node.js version 21.

Proceed with the installation. Now you must execute the installer that was downloaded from the official site. Launch it and adhere to the on-screen guidelines for a smooth setup process.

Check again your Node.js version: It is good to confirm if you have the correct Node.js version 21 to be on the right track. You must make sure that Node.js 21 is installed on your system.

If you follow these simple instructions, you will be able to update your system to the newest version of Node.js 21 and fully utilize its most recent features and enhancements.

Conclusion

The new version of Node.js 21, has arrived with features that make creating programs easier and more comfortable. When you update to the newest version of Node.js, the Node.js 21 update, you receive many advantages, including improved flexibility and better performance. Therefore, it is essential to run through the upgrade for the latest version.



How much is a great User Experience worth to you?


Browsee helps you understand your user's behaviour on your site. It's the next best thing to talking to them.

Browsee Product