Olibr Blogs

Blog > All Engineering Topics > What is New in Node.js 21?

What is New in Node.js 21?

Node.js 21: Latest Update and Features

by Snehal Naik
What's Node.Js 21
Pointer image icon

Introduction

JavaScript Runtime environments, such as Node.js, help users access built-in libraries and objects to write code in JavaScript for server-side scripting. The release of Node.js 21 is an important event in the Node.js community. Let’s see what is new in Node.js 21 in this blog.  

best software companies

Don't miss out on your chance to work with the best!

Apply for top job opportunities today!

Pointer image icon

What is Node.js 21?

Node.js Release Schedule

Source: Node.js.org 

Node.js is an asynchronous event-driven JavaScript runtime that helps you build scalable network applications. Node.js v21, the latest Node update available as a stable release. The latest release is focused on optimized runtime execution and decreased memory overhead, which offers better performance. Node.js 21 will be Current’ release until April 2024. Some key highlights of the latest release include stable fetch and WebStreams and a new experimental flag to flip module defaults (–experimental-default-type). You can download Node.js 21 in various ways here

Pointer image icon

Node.js Versions

Node.js Version Codename Release Date npm
v21.7.2 2024-04-03 v10.5.0
v20.12.1 Iron 2024-04-03 v10.5.0
v19.9.0 2023-04-10 v9.6.3
v18.20.1 Hydrogen 2024-04-02 v10.5.0
v17.9.1 2022-06-01 v8.11.0
v16.20.2 Gallium 2023-08-08 v8.19.4
v15.14.0 2021-04-06 v7.7.6
v14.21.3 Fermium 2023-02-16 v6.14.18
v13.14.0 2020-04-29 v6.14.4
v12.22.12 Erbium 2022-04-05 v6.14.16
v11.15.0 2019-04-30 v6.7.0
v10.24.1 Dubnium 2021-04-06 v6.14.12
v9.11.2 2018-06-12 v5.6.0
v8.17.0 Carbon 2019-12-17 v6.13.4
v7.10.1 2017-07-11 v4.2.0
v6.17.1 Boron 2019-04-03 v3.10.10
v5.12.0 2016-06-23 v3.8.6
v4.9.1 Argon 2018-03-29 v2.15.11
v0.12.18 2017-02-22 v2.15.11

Source: Node.js.org 

Pointer image icon

What’s New in Node.js 21?

The latest version has many new features and fixes that are part of the existing LTS releases. These are all evolving as the new version continues to make progress. For this reason, the changelog for Node.js 21 includes only a small subset of the features and work since the last major release. The changes are related to: 

  • Semver-Major Commits 
  • Semver-Minor Commits 
  • Semver-Patch Commits 
Pointer image icon

Key features of Node.js 21

Stable fetch/WebStreams

The latest version impacts WebStreams, FormData, Headers, Request, Response, and fetch. The release of Node.js 21 promotes the Fetch API to a stable release. This allows users to use Fetch and adopt it in their applications. 

Stable fetch

The Fetch API implementation depends on the AbortController interface to abort fetch requests. It also needs a browser-compatible Web Streams API. Both were added in v15.0.0 and v16.5.0, respectively. The AbortController became stable in v15.4.0 but the Web Streams API has been stabilized in v21. 

WebStreams

Built-in WebSocket client

Node.js 21 offers an experimental browsercompatible WebSocket implementation enabled through the flag: –experimental-websocket. This update complements existing web platform APIs such as the Fetch and Web Streams APIs, AbortController APIs, the WHATWG URL parser, and more. It is not ready for production use yet, but you can start experimenting with it.  

Built-in WebSocket client

V8 11.8

With the V8 engine upgraded to v11.8, you can expect improved performance and JavaScript features such as: 

  • Using Object.groupBy() and Map.groupBy() for array grouping to combine like data into groups 
  • (ArrayBuffer.prototype.transfer), new programmatic API, which helps transfer ownership of ArrayBuffers, optimize reallocations, and fix resizable ArrayBuffers into fixed-length ones 
  • WebAssembly extended-const expressions 

Support for globs in the Node.js test runner

The latest version of Node.js has support for glob expressions in the test runner when you specify the –test parameter. This allows you to use powerful glob patterns to run tests more efficiently and flexibly. For example, you can use a command like node –test **/*.test.js to execute tests for all files with the .test.js extension across multiple directories.  

ECMAScript Modules (Esm)

There are many benefits of ES modules over CommonJS modules. They offer compatibility with multiple modules, better performance, tree-shaking support, and namespaces. With Node.js, you can treat files with a .JS extension by default as a CommonJS module. It improves the compatibility of CommonJS. Additionally, ES modules are solved and cached based on URL semantics, loaded synchronization, and support dynamic imports. 

NPM 10.2.0

NPM 10.2.0 uses new package resolution algorithms in Node.js 21. This offers new safety features to support security vulnerabilities. NPM 10.2.0 contributes to faster package installation and resolution. It also supports the package-lock.json file, which locks down project dependencies and accommodates unexpected modifications. 

llhttp 9.1.2 strict mode enforcement

Node.js 21 now by default enables all the previous settings that were earlier included in strict mode. It is now mandatory to have \r\n after headers. Also, to ensure consistent data processing users have to add \r\n after a chunk. For better adherence to protocol and improved connection handling, data transmission after a message with the Connection: close header has been parsed is no longer allowed  

Object integration

Node.js 21 enhances web interoperability with the global navigator object. This change allows developers to access hardware concurrency information through navigator.hardwareConcurrency.

Pointer image icon

How to Migrate from Node js 20 to Node js 21?

Update .json Package

  • Open your project’s package.json and go to the engines field.  
  • Update the version of Node.js to “>=21.0.0” to ensure compatibility.  

Update Dependencies

Check and update devDependencies or any other dependencies that need updates as per the latest Node.js 21 version. All project dependencies must be reviewed during migration to ensure compatibility with Node js 21. You can use Yarn or NPM to upgrade packages to their latest versions 

Test, Optimize, Update, and Deploy Application

It is important to test the application thoroughly on Node.js 21 after you have updated the dependencies. This involves checking for compatibility problems, deprecated APIs, or behavior changes. It is also important to optimize the code so that your project can benefit from the new features in Node.js 21. 

Pointer image icon

Difference between Node.js 20 and Node.js 21

Aspects Node.js 20 Node.js 21
Long-Term Support Now offers LTS with security updates and critical bug fixes for an extended period Still in the Current phase and will receive updates but not the same level of stability as an LTS release
Performance Node.js 20 offers efficient memory management and garbage collection Offers better memory management and garbage collection than Node.js 20
Feature Set Does not have experimental async module loading feature Has the experimental async module loading feature
Pointer image icon

Wrapping Up

As an open-source, cross-platform JavaScript runtime environment, Node.js 21 offers tons of benefits. The newest update has positioned Node.js as a robust JavaScript runtime with many improvements. The complete list of new features and bug fixes along with the other changes can be found in the official Node.js 21 release notes. As you explore the latest Node.js 21 features, you will see how it includes performance optimizations, enhanced tooling, better language features, and standard library additions. If you are a developer seeking exciting job opportunities that match your talent and skillset, sign up with Olibr for fulfilling career opportunities 

Take control of your career and land your dream job!

Sign up and start applying to the best opportunities!

FAQs

Yes, Node.js is a free, open-source JavaScript library. Developers can use it to create servers, web apps, command line tools, and scripts. 

Middleware in Node.js is a function in the application request-response cycle. It has access for responding to an object, requesting an object, and moving to the next middleware function.

You can connect MongoDB with Node.js by installing MongoDB driver using NPM, followed by importing MongoClient at the top of your file. Then you need to create a new MongoClient instance and pass your connection string as a parameter. 

Node.js uses Single Threaded Event Loop architecture and an asynchronous runtime environment to run server-side applications with JavaScript. 

You may also like

Leave a Comment