Node.js 21 New Release: What’s New and How to Upgrade

Node.js 21 New Release What's New and How to Upgrade


Node.js has rolled out its latest version, Node.js 21, packed with a host of new features and enhancements.

In this blog post, we’ll delve into the significant changes that Node.js 21 brings to the table and guide you on how to seamlessly upgrade to this new version.

Exciting New Features in Node.js 21

Node.js 21 introduces several noteworthy features, including:

1. V8 11.8: Node.js 21 incorporates the latest V8 JavaScript engine version, V8 11.8. This upgrade brings substantial performance improvements and introduces new features that promise to enhance your development experience.

2. Stable fetch and WebStreams: In Node.js 21, the fetch and WebStreams modules have reached a stable state. This means you can confidently utilize these modules in production environments, knowing they are fully supported.

3. Experimental default module type: Node.js 21 presents an experimental feature, denoted by the flag --experimental-default-type, allowing you to transition the default module type from CommonJS to ESM (ECMAScript modules). This transition is a forward-looking step to align your code with the future, as ESM is slated to become the default module type in Node.js.

4. Built-in WebSocket client: One of the standout additions in Node.js 21 is the inclusion of a built-in WebSocket client. This new feature simplifies the development of real-time web applications, making the process smoother and more efficient.

5. Test Runner Enhancements: Node.js 21 has revamped its test runner with several updates. Notable improvements include bolstered support for ES modules and enhanced error reporting, promising a more robust testing experience.

Upgrading to Node.js 21

If you’re eager to make the leap to Node.js 21, follow these straightforward steps:

1. Download the Node.js 21 Installer: Head to the official Node.js website and procure the Node.js 21 installer.

2. Run the Installer: Execute the installer and adhere to the on-screen instructions for a hassle-free installation process.

3. Verify Your Node.js Version: After the installation concludes, it’s a good practice to verify that you are indeed running Node.js 21. Confirm this by running the following command:

These steps will ensure a smooth transition to Node.js 21, empowering you to take full advantage of its latest features and improvements. Happy coding!

node --version

Let see new features of Node.JS 21 in action

Let’s dive into some practical examples of how you can make the most of the new capabilities in Node.js 21.

stable fetch and WebStreams module

One standout feature is the stable fetch and WebStreams modules, and we’ll demonstrate their power through a simple code snippet.

Below, you’ll see a practical illustration of how you can harness the potential of the fetch and WebStreams modules to effortlessly download a file and seamlessly stream it to your console. This showcases the versatility and enhanced functionality Node.js 21 brings to the table.

import fetch from "node-fetch";
import { TextDecoderStream } from "web-streams-polyfill";

const url = "";

const response = await fetch(url);
const decoderStream = new TextDecoderStream();

const reader = response.body.getReader();
const writableStream = decoderStream.writable;

const writer = writableStream.getWriter();

while (true) {
  const chunk = await;
  if (chunk === null) {



const text = await;


Experimental default module type

The introduction of the --experimental-default-type flag in Node.js introduces a valuable feature that allows users to determine how files are interpreted, offering a choice between “commonjs” and “module”. When set to “module”, it brings about the following changes:

  1. Implicit ES Module Interpretation:
    Any code that is directly entered or sent to Node.js will be automatically treated as an ES module, unless specified otherwise.
  2. Auto-Detection of ES Modules:
    When there is no package.json file present in the directory or any of its parent directories, Node.js will interpret .js files and files lacking extensions as ES modules.
  3. Undefined Module Type:
    In cases where a package.json file exists but does not specify the module type and the directory is not within the node_modules, Node.js will interpret .js files and files without extensions as ES modules.

In addition to the above, the --experimental-wasm-modules flag introduces the capability to recognize files without extensions containing a specific WebAssembly (Wasm) header as Wasm modules.

One significant point to note is that this new ES module loading mechanism is applied from the very beginning of the Node.js execution. This means that it’s no longer possible to modify how Node.js initiates modules using the --require flag.

In summary, the introduction of the --experimental-default-type flag enhances the flexibility of Node.js in handling modules. It simplifies the adoption of ES modules and WebAssembly, all the while ensuring compatibility with the existing CommonJS module system. This feature represents a significant step forward in Node.js’s evolution.

Using the experimental default module type, you can start node.js with the following flag:

node --experimental-default-type=esm

once you have started node.js with this flag, you can import ES module using the following syntax:

import { MyClass } from "./my-class.mjs";

Built-in WebSocket client

In this example explore the process of utilizing the built-in WebSocket client for establishing a connection with WebSocket server:

import { WebSocket } from "ws";

const url = "ws://localhost:8080";

const socket = new WebSocket(url);

socket.onopen = () => {
  console.log("Connected to WebSocket server.");

socket.onmessage = (message) => {
  console.log("Received message from WebSocket server:",;

socket.onerror = (error) => {

socket.onclose = () => {
  console.log("Disconnected from WebSocket server.");

Test Runner Enhancements

During testing in Node.js, there is an automatic file selection process based on predefined patterns such as /.test.?(c|m)js.

Starting from version 21, users have the option to define one or multiple glob patterns as the final argument(s) in the command. Here’s an illustrative example of how it works: you can run the command ‘node –test */.test.js */.spec.js’. This new feature brings increased flexibility and control to end users. It empowers users by allowing them to customize their testing procedures to align with their unique project requirements and structures.

What else?

Node.js version 21 brings along a host of additional enhancements, even though many of them may not have an immediate impact on everyday users. I encourage you to explore these updates in detail within the official release notes for a more comprehensive understanding

Leave a Reply

Your email address will not be published. Required fields are marked *