Web Development
Published: Oct 19, 2023

What’s New in Node.js 21? Explore New Features of Node.js in The New Version

Verified
Verified Expert in Project management
Faisaluddin is a Project Lead passionate about successful software delivery.
NodeJS21 - New Version

Quick Summary: The Node.js community always breaks barriers with its latest features and updates, following the same tradition and with the grand success of Node 20. With Node.js 21, the Node.js community has returned with its latest release and version. In this article, we are going to talk about the improvements and updates included in Node JS 21. Furthermore, we've noted down the updates that NodeJS 21 has brought for the dev community.

The year 2023 is about to end soon. But how can we bid adieu without talking about the major updates of NodeJS?

It’s the time of year when every Node.js developer finds themselves in their comfort zone with the latest and new releases of Node.js. And it’s the latest version of Node JS – Node.js 21.

Node.js 21 replaces Node.js 20 as our current release line, and Node.js 20 is being promoted to long-term support (LTS). As per the release schedule, Node.js 21 will be ‘Current' release for the next 6 months, until April 2024.

Build 10X Fast And Responsive Web Applications 03

On This Page
  1. Introduction to NodeJS 21
  2. What’s New in Node.js 21?
  3. The Promotion of Node.js 20 to LTS
  4. Get Started with Node JS 21 Today!

Introduction to NodeJS 21

Node.js has released Node JS 21, the latest version in its series of innovative releases. The release date of Node.js 21 was 17th October 2023. The latest version of NodeJS comes with many internal improvements and a few user-facing new features, including the introduction of a stable fetch API. Furthermore, Node.js 20 is now stable and supported for developers as it has moved into a long-term support release.

The experienced backend developers don’t require any introduction to Node.js due to its amazing functionalities and versatility.

Therefore, if you are planning to upgrade your tech stack with Node.js 21, you can install the latest version of Node.js on your system or upgrade it in simple steps.

The addition of the fetch API is the main feature of Node.js 21. This brings a standardized way of making HTTP requests, which allows developers to simplify the entire process.

The good news for NodeJS developers is that they don’t need to rely on third-party libraries or complex code to send HTTP requests and handle responses.

You can discover the whole release schedule in detail here. In fact, we will cover some of the most prominent internal improvements which Node JS 21 comes with, in this article. One noteworthy update for dependencies is V8 to v11.8. You can find the notable changes about V8 in the roadmap.

“If you’re interested in getting access to interesting new features early, Node.js 21 is a great way to test and see what’s coming. Our release schedule specifically covers this. If you’re already in active deployment or if you are planning for it, Node.js 20 and 18 LTS are for you,” said Rafael Gonzaga, Node.js Technical Steering Committee (TSC) Member. “Many thanks to our open source contributors for making Node.js better and better. Thanks also to OpenSSF and Project Alpha Omega for helping us improve Node.js security.”

What’s New in Node.js 21?

Well, since the release of Node.js 20, more than 1153 commits made it possible to release of Node.js 21. There were many developers' efforts, and bug-fixing tasks made it a successful release. Let’s understand what’s new and major updates in Node JS 21.

Latest features and major updates in Node JS 21:

  1. Fetch API Promoted to Stable
  2. Experimental Support for WebSocket API
  3. New --experimental-default-type Flip Module
  4. New V8 JavaScript Engine Version V8 11.8
  5. Experimental Support for Better Default for ESM
  6. Add Flush Option to fs.writeFile() Functions
  7. Update on llhttp Version 9.1.2
  8. Partial Navigator API Implementation

Fetch API Promoted to Stable

With Node.js 21, the Fetch API has been promoted to the stable version. This actually streamlines the development of JavaScript, which runs on both the frontend and backends, by enabling Web API in Node.js.

Experimental Support for WebSocket API

The support for WebSocket API comes in experimental mode with Node 21. This actually empowers you to connect to a WebSocket server from a Node.js application without resorting to third-party packages.

You can access the WebSocket function once you run the node --expertimental-websocket. This implementation uses the undici websocket and is spec compliant.

This is an important innovation since it allows real-time, two-way interactive communication between the client and server by natively integrating WebSocket capabilities into Node.js. This helps you to create real-time NodeJS applications, like Game Applications, Chating Applications, and Live Updates for websites. Hence, this could be the reason to go for Node.js development.

Let’s understand it with the coding example here:

const ws = new WebSocket('wss://echo.websocket.events/')

ws.addEventListener('message', (event) => {
console.log('received:', event.data) // "echo.websocket.events sponsored by Lob.com"
})

ws.addEventListener('open', () => {
let i = 0
setInterval(() => {
const text = `Hello! #${i} is your number`
console.log('sending:', text)
ws.send(text)
i++
}, 1000)
})

You can execute this code with node --experimental-websocket example.js to connect to a WebSocket server, send messages to it, and get responses if you save it in a file called example.js.

New --experimental-default-type Flip Module

Now you can easily set how files are interpreted: as “commonjs” or as “module” with new --experimental-default-type flag in Node.js 21.

This is what happens when you use it set to module:

  • Unless you specify differently, Node.js will interpret any code you enter or send it as an ES module.
  • The folder (or any parent folder) containing no package.json will recognize .js files or files without an extension as ES modules.
  • Files with .js extension or without an extension are recognized as ES modules if the package.json file is present but does not specify which kind of module to use, and the folder is not located inside node_modules.

Furthermore,

  • Any file without an extension that contains a specified Wasm header will be recognized as a Wasm module if you use the --experimental-wasm-modules flag.
  • The new method of loading ES modules is utilized from the beginning. Using --require to change how Node.js launches modules is no longer possible.

In a nutshell, this flag preserves backwards compatibility with CommonJS while enabling a more flexible module system handling in Node.js, making it easier to use ES modules and WebAssembly.

How to maximize your web app's potential with NodeJs 01

New V8 JavaScript Engine Version V8 11.8

JavaScript code is executed in an environment provided by the V8 JavaScript engine, which enables cross-platform compatibility. This removes the need for developers to worry about platform characteristics, including the underlying hardware architecture (x64, Arm, or Power PC) or the operating system (Linux, Windows, macOS).

V8 is maintained on s390 and Power PC by Red Hat's Node.js team members, who commit changes weekly to ensure V8 stays operational on these systems.

Experimental Support for Better Default for ESM

A complete ESM implementation is better suited by defaults that are enabled by the new --experimental-default-type options. This keeps the project's ongoing efforts to enhance the developer experience for ESM users while preserving CommonJS compatibility. While the research determines the best solutions for the ESM developer experience, other strategies are also being examined, such as automatically detecting ESM when possible.

Add Flush Option to fs.writeFile() Functions

With this modification, the fs.writeFile family of Node.js function now has a flush option. When you use the flush option, after a successful write transaction, data is compelled to be flushed to permanent storage, avoiding stale data during subsequent read operations.

Update on llhttp Version 9.1.2

With the most recent version – NodeJS 21, the HTTP parser now has the ability to activate extra leniency flags. This makes it possible for llhttp to communicate more easily with other non-standard HTTP implementations.

Nevertheless, apps are vulnerable to request smuggling or poisoning assaults if these leniency flags are enabled. It is therefore strongly advised to avoid using this option unless there is a very good reason to.

To enable leniency flags is, you can use this flag: --insecure-http-parser

Partial Navigator API Implementation

It is common to use the Navigator in front-end settings. Navigator.hardwareConcurrency is available in Node.js v21. Additional Navigator APIs will likely be made available in later versions.

NodeJs for enterprise web app

The Promotion of Node.js 20 to LTS

It's advisable to use the latest release of Nodejs – Nodejs 21 from next week – after being promoted to LTS.

As with every release, Node.js comes with new features and functionalities that make upgrading a wise choice for NodeJS app development.

Here, we are going to display some new features in Node.js 20 that have caught our attention. Though they may not be strictly new in Node.js 20, they are nevertheless noteworthy for having landed since the last major release promoted to LTS, given how quickly features are incorporated back into the current Node.js release line.

Here are some few points to consider:

  • Stable Test Runner
  • WASI (Experimental) no longer requires a command line flag, and version is required
  • Permission Model (Experimental)
  • Single Executable Applications (Experimental)
  • Tracing Channel (Experimental)

Get Started with Node JS 21 Today!The latest release of Node.js 21 brings some exciting features to make the development experience smoother and hassle-free. That’s the reason, as an emerging web app development company, we are all ready to utilize Node.js 21 for our upcoming projects. We know that NodeJS 21 will help you overcome bottlenecks and challenges faced in the development.However, if you find any issues while installing NodeJS 21 on your system or, as a business owner, if you wish to migrate your Node.js project to the latest version, we have you covered. You can definitely contact our Node.js experts and share your requirements.

Don't Forget to share this post!

Faisaluddin Saiyed

Faisaluddin Saiyed

Verified
Verified Expert in Project management
View All Posts

About the Author

Faisaluddin is a dynamic Project Orchestrator passionate about driving successful software development projects. His enriched 11 years of experience and extensive knowledge spans NodeJS, ReactJS, PHP & frameworks, PgSQL, Docker, version control, and testing/debugging. Faisaluddin's exceptional leadership skills and technical expertise make him a valuable asset in managing complex projects and delivering exceptional results.