Everything About NodeJS 22: New Features and Latest Updates
Quick Summary
The Node.js development team has raised the bar of efficiency to the next level with the latest version of NodeJS. As with previous versions, this time, the team has left no stone unturned to bring the best for the development community worldwide. This blog post will examine what’s new in NodeJS 22 features and updates. We will also explore notable changes with the latest NodeJS v22 that will convince you to Upgrade to the latest version of Node 22.
Table of Contents
Introduction
Node.js 22 is now available in the market. It offers many of the latest features and updates, which seems greatly enticing to the developer community. This time, the development team at Node has kept the Node 22 release date at April 24, 2024. The Node v22 will enter long-term support (LTS) in October 2024, when the development team will follow the traditional pathway. Until then, it will remain the “Current” release for the next six months. But now, considering What’s New in NodeJS 22 Features and Updates, let us jump to it.What’s New in NodeJS 22 Features and Updates
The latest version of NodeJS surprises developers and business owners worldwide by introducing new features and fixing issues, highlighting require()ing ES modules, WebSocket client, updates within the V8 JavaScript engine, and more!Updating V8 to 12.4 and Maglev Compiler
The V8 release 12.4 now includes add-on features such as WebAssembly Garbage Collection Array.fromAsync, Set methods, and iterator helpers. The V8 Maglev compiler (the new JIT compiler) is now enabled by default on supported architectures. The Maglev helps improve the performance of short-lived CLI programs. It sits between the existing Sparkplug and Turbofan compilers and has been enabled by default on supported architectures. The latest version of Node v22 streams performs better, with the default high watermark increased from 16 KB to 65 KB. This uses more memory, but you can easily adjust it for memory-sensitive environments using setDefaultHighWaterMark(). The fetch() API and the test runner also run faster because they create a more efficient AbortSignal instance. Also, the synchronous filesystem APIs have been enhanced for better performance.Still Using The Old Version of Node.JS Framework? Hire NodeJS developers to upgrade today and enjoy the latest features and updates that Node v22 can offer your business application.
ESM Compatibility Improvement
NodeJS 22 has improved ESM compatibility with their continued focus with a few tech preview additions, including import.meta.dirname and Import.meta.filename, which were introduced within the Node.js 21 lifecycle. Know More About Node.js Releases. Another item on the list that focuses on simplifying the migration path from CJS to ESM is the—- experimental-detect-module flag. This flag allows you to run the ES modules automatically when their syntax can be detected. When the files have a .js or no extension and no specified type in package.json, it analyzes the file syntax. In case of such uncertainty, it treats it as CommonJS but runs accordingly when it is an ES module. Also, Node.js 22 simplifies the transition from the CJS to ESM. Now, you can require the synchronous ESM graphs, but the modules must be explicitly marked as ES modules and should load fully synchronously. Also, it should be noted that the import assertions have been replaced with the import attributes in Node v22 and are no longer supported. To update your code to the new syntax, replace the “assert” with “with.” For Illustration: The original code – import “foo” assert { … } To be replaced with – import “foo” with { … }Better Handling of Bad IPv6 Configurations
Since the launch of Node.js 17, the team has stopped favoring IPv4 over IPv6 and has followed the orders returned by the DNS entries. This change aims to handle bad IPv6 configurations better and will not affect the functionality of properly set-up hosts. However, you might face issues with the partially incorrect or configured IPv6 stack on your hosts. Since the launch of the Node 17 version, the team has been focusing on improving the experience with incorrectly configured IPv6 stacks. This includes implementing the happy eyeballs algorithm and fixing edge cases, most recently adding new connection attempt events. When encountering problems, you can always revert to the old behavior using the ‘ – – dns-result-order=ipv4first ’ command-line option. Also, there is the ‘ – – no-network-family-autoselection’ option, which is enabled by default but can be used to disable the happy eyeballs algorithm.Deprecations in util
The major releases within the Node 22 launch act as an opportunity to deprecate or remove obsolete APIs. The development project follows a planned deprecation process with three levels: Documentation-only, runtime, and end-of-life. Within the Node.js 22, runtime deprecated several util APIs. You can check for already deprecated APIs by running your app with the – – throw-deprecation for getting a runtime error as the runtime deprecated API is used. It is an excellent time to check your usage of the util APIs when upgrading to NodeJS 22 and to remove the usage of the runtime deprecated APIs.Wath Mode Command Line Option (Stable)
The command line option for watch mode is now stable. Within the previous Node 19 version, the development team at Angular discussed the – – watch as an exciting addition to the feature list. Now, with the Node 22 version, it has been promoted to stable. The other options include – – watch– – watch-path
– – watch-preserve-output The core advantage of the said feature is the development process becoming time efficient as the application restarts automatically as you make changes to the watched files.
WebSocket
The browser-compatible implementation of WebSocket will now be enabled by default, which was previously behind the flag – – experimental-websocket. This means that Node.js 22 will include a WebSocket client without relying on external dependencies.Platform Support
This segment keeps running with the same minimum supported levels for the platforms and tooling used to create Node.js as in the Node.js 21 version. The NodeJS building instructions document the minimum levels.Add-on APIs to Reduce Additional Dependencies
Recently, the project has introduced several APIs focusing on essential functionalities without additional dependencies. This includes tools such as an argument parser, test runner, web socket, and the Text Styling API, which was included amid the development of Node.js 21. The Text Styling API allows text formatting based on util.inspect.colors. Example – const { styleText } = require(‘node:util’);const myMessage = styleText(‘red’, ‘This text is orange’);
console.log(myMessage); Other improvements include the support for defining the environment variables in files. This was achieved through the – – env-file command line option or programmatically using the process loadEnvFile and util.parseEnv developed during Node v21. The functionalities help facilitate seamless loading of the .env files or the content in .env format. Example – const { loadEnvFile } = require(‘node:process’);
loadEnvFile();