Satguru Technologies

As 2023 comes to a close, it’s essential to discuss the significant updates in the world of Node.js. This is the time of year when Node.js developers eagerly embrace the latest releases. The most recent version to talk about is Node.js 21.

Node.js 21 has taken the place of Node.js 20 as the current release line, with Node.js 20 now being elevated to long-term support (LTS). Following the release schedule, Node.js 21 will remain the ‘Current’ release for the upcoming six months, extending until April 2024.

Introduction to NodeJS 21

Node.js has recently introduced Node JS 21, the newest version in its ongoing series of releases. This version, Node.js 21, was officially released on October 17, 2023. It brings a host of internal improvements and a handful of user-facing features, including the introduction of a reliable fetch API. Moreover, Node.js 20 has now reached a stable state and is available with long-term support, providing a dependable choice for developers.

If you’re considering enhancing your tech stack with Node.js 21, you can easily install this latest version on your system or perform a straightforward upgrade.

The primary highlight of Node.js 21 is the addition of the fetch API, which establishes a standardized method for performing HTTP requests. This simplifies the entire process for developers, eliminating the need for third-party libraries or intricate code to manage HTTP requests and responses.

For an in-depth look at the complete release schedule, you can find detailed information here. In this article, we will delve into some of the significant internal enhancements introduced in Node JS 21. One notable update involves the upgrade of dependencies, specifically V8 to version 11.8.

What’s New in Node.js 21?

Let’s explore the fresh features and significant changes in Node.js 21. Since the launch of Node.js 20, a remarkable 1153 commits have paved the way for the release of Node.js 21. This achievement is the result of the dedicated efforts of numerous developers and the successful resolution of various bug-fixing tasks. Now, let’s delve into the noteworthy updates and new elements in Node JS 21.

Stable Fetch API Status

The Fetch API in Node.js had previously been introduced with an experimental status, starting from version 17.5.0. Initially, it required an –experimental-fetch flag to enable it, providing access to features like fetch, headers, requests, andresponsese, making it browser-compatible. From versions 18.x to 20.x, this flag became unnecessary, but the Fetch API retained its experimental label.

Now, with the release of Node.js 21, the Fetch API has gained stable status. This means that if you were hesitant to use Fetch in your applications due to its experimental nature, you can now confidently use it in your projects without concern.

Web Streams API Is Stable

The Fetch API relies on the AbortController interface for aborting fetch requests and also requires a browser-compatible Web Streams API, which was introduced in versions 15.0.0 and 16.5.0, respectively. While the AbortController became stable in version 15.4.0, it’s noteworthy that the Web Streams API has just reached stability in version 21.

It’s important to note that the older Node.js streams API has not been deprecated or removed. It will continue to exist alongside the new API. Additionally, it offers a means to convert between web streams and the older API using the.fromWeb() and.toWeb() methods.

A Built-in WebSocket Client

Node.js is following a pattern of implementing standard web platform APIs, and it has now introduced a WebSocket global, following the WHATWG standard, with the help of the –experimental-websocket flag. This addition aligns with other web platform APIs already present in Node.js, including Fetch and Web Streams APIs, AbortController APIs, the WHATWG URL parser, EventTarget, Web Crypto API, and more.

ES Modules Improvements

In Node.js 21, a new feature is introduced through the –experimental-default-type flag, enabling you to modify the default behavior for input files that are not explicitly designated as ES modules or CommonJS. Prior to this update, such files were automatically treated as CommonJS. By using –experimental-default-type=module, you can opt for a full ES module implementation in the following scenarios:

  • When you provide string input via –eval or STDIN, especially when –input-type is not specified.
  • For files with .js extensions or no extensions, in cases where there is no package.json or the nearest parent package.json lacks a type field (excluding scenarios in node_modules).

These enhancements are a step toward eventually supporting ES module syntax as the default with minimal disruptions.

Test Runner Improvements

Expanding on its stabilization in Node.js v20, the native test runner receives additional improvements in version 21. Notably, it has been enhanced to include support for glob expressions, enabling users to conveniently match multiple test files using custom glob patterns in addition to the predefined defaults.

Module Customization Improvements

Module customization in this release has been made more straightforward by eliminating the perplexing globalPreload hook. Instead, it introduces the register API for transmitting data from the application thread to the customization hooks and the initialize hook for setting up communication channels between threads.

llhttp v9.1.2 Strict Mode Enforcement

With the release of llhttp v9.1.2 in Node.js, strict mode enforcement has become the default setting. This default configuration brings in strict mode features, enhancing code reliability and security. 

Notable modifications encompass the requirement of \r\n presence after headers and chunks, which ensures uniform data processing. Additionally, data transmission after a message with the Connection: close header is no longer permitted, resulting in improved protocol adherence and better connection management.

For compatibility with older versions, you have the option to disable these modifications by using the –insecure-http-parser flag.

Performance Improvements to Key APIs

In this release, the Node.js performance team has introduced notable improvements to key APIs. Specifically, they have made significant enhancements to the Streams API, resulting in a roughly 10% speed increase. These optimizations involve eliminating redundant checks, using bitmaps, and more efficient scheduling of callbacks.

Additionally, the update has tackled the issue of uncorking responses by avoiding the splitting of chunked responses into multiple parts. This adjustment contributes to a reduction in unnecessary overhead for both the client and server.

V8 Upgraded to v11.8

In this update, the V8 engine has been advanced to version 11.8, introducing enhanced performance and several new JavaScript features, including:

  • Array grouping, which enables the combination of similar data into groups through functions like Object.groupBy() and Map.groupBy().
  • The introduction of a programmatic API (ArrayBuffer.prototype.transfer) designed for transferring ownership of ArrayBuffers, optimizing reallocations, and transforming resizable ArrayBuffers into fixed-length ones
  • An expansion of WebAssembly with extended-const expressions

NPM Upgraded to v10.2.0

Node.js version 21 now includes npm version 10.2.0, bringing with it a noteworthy addition of the ‘sbom’ command. This new command enables you to create a Software Bill of Materials (SBOM) for your current project. For further details on the latest updates in recent npm releases, you can refer to the GitHub documentation.

Node.js 21 Miscellaneous Updates

In Node.js version 21, there are several additional enhancements and updates, including the following:

A partial implementation of the global navigator object, which provides access to hardware concurrency information through navigator.hardwareConcurrency.

An added ‘flush’ option in the fs.writeFile() function family ensures that data is written to disk at the conclusion of a successful write operation, preventing the need to read redundant data.

Should You Use Node.js v21 in Production?

Using Node.js version 21 in a production environment is not advisable. This particular release, being an odd-numbered October release, is only supported for a one-year period. In contrast, the even-numbered April releases like 16, 18, and 20 receive long-term updates spanning up to three years.

Upgrading to Node.js v21

If you intend to install or upgrade to Node.js version 21, you can do so by visiting the Node.js download page and selecting the appropriate link for your operating system and architecture.

Wrapping Up

In conclusion, Node.js version 21 brings a wide array of enhancements, including improvements in tooling, language features, additions to the standard library, and optimizations for better performance. These updates solidify its standing as a powerful JavaScript runtime and bring it in line with contemporary alternatives such as Deno and Bun.