Node.js 22 has arrived with a host of exciting new features and improvements that aim to enhance the developer experience, streamline workflows, and optimize performance. In this article, we'll dive into some of the most notable additions and how they can benefit your Node.js projects.
Mixing Module Systems with require()ing ESM Graphs
One of the standout features in Node.js 22 is the ability to require ECMAScript modules (ESM) directly within CommonJS modules using the --experimental-require-module
flag. This allows you to seamlessly integrate different module systems in your applications, providing more flexibility in managing dependencies and code organization.
// CommonJS module
const { sayHello } = require('./esm-module.mjs');
sayHello(); // Hello from ESM!
Built-in WebSocket Client for Real-Time Communication
Node.js 22 includes a native WebSocket client, eliminating the need for external libraries like the ws
module. This built-in capability simplifies the development of applications that require live data updates, such as chat apps or live notifications.
const { WebSocket } = require('node:ws');
const client = new WebSocket('ws://example.com/websocket');
client.on('open', () => {
console.log('WebSocket connection established');
client.send('Hello, server!');
});
Leveraging V8 JavaScript Engine Updates
Node.js 22 incorporates updates to the V8 JavaScript engine, bringing features like WebAssembly Garbage Collection, Array.fromAsync
, Set methods, and iterator helpers. These enhancements provide more tools for developers to write efficient and expressive code.
Array.fromAsync({
length: 3,
0: Promise.resolve(1),
1: Promise.resolve(2),
2: Promise.resolve(3),
}).then((array) => console.log(array));
// [1, 2, 3]
Streamlining Scripts with Direct Execution of package.json
An experimental feature in Node.js 22 allows you to execute scripts directly from the package.json
file using the node --run <script-in-package-json>
CLI flag. This simplifies script management and execution, improving development workflows and making it easier to run specific tasks.
{
"scripts": {
"start": "node app.js"
}
}
node --run start
Enhancing Streaming Performance
Node.js 22 adjusts the default High Water Mark from 16KiB to 64KiB, improving overall performance for streaming operations. While this may result in a slight increase in memory usage, it can significantly boost the efficiency of your streaming-based applications.
const fs = require('node:fs');
const readStream = fs.createReadStream('large-file.txt', {
highWaterMark: 64 * 1024, // 64 KiB
});
These new features and improvements in Node.js 22 demonstrate the ongoing efforts to enhance the developer experience, streamline workflows, and optimize performance for various types of applications. As you explore and adopt these features in your projects, you'll be able to write more efficient, expressive, and maintainable code.
To learn more about Node.js 22 and its capabilities, visit the official Node.js documentation at https://nodejs.org/docs/latest-v22.x/api/.
Happy coding with Node.js 22!
Top comments (2)
Great read Andreas 🙂 Yes, the native WebSocket support is really good news! It's a great LTS version! I've already updated my docker containers to support and use those great features 😃
Nice stuff