A new WebSocket client and a new V8 compiler are among the updates to Node.js 22, in the first of two major releases planned this year.
Node.js is an open source, cross-platform runtime that allows developers to build server-side applications and tools with JavaScript. The New Stack spoke with Rafael Gonzaga, a principal open source engineer at NodeSource and a member of the Node.js Technical Steering Committee, about the updates in the spring release.
“With this update, we get some interesting stuff, including new array methods, new set methods, some helpers on iterators,” Gonzaga said. “Apart from that, we have moved the Node.js watch module, which is kind of a replacement for Nodemon, to stable so users can now rely on that on their prediction usage.”
Nodemon is a tool that automatically restarts a node application when it detects file changes in the directory.
V8 Updated
V8 is Google’s open source JavaScript and WebAssembly engine. It’s written in C++ and used in Node.js. Node.js 22 includes updates to the V8 JavaScript engine, Gonzaga said, including adding Maglev, a new compiler. Maglev was introduced by V8 in December 2023, and is enabled by default on some supported architectures, he added.
“It should produce a highly optimized code and for some users on supported architectures these should improve the performance by a lot, optimizing the CPU usage and directly improving the throughput of your applications,” he said. “I’m still doing research on performance to see the impact of this feature for regular users or regular applications, HTTP applications, and I should publish it soon; but in theory, this will improve the performance of Node.js apps.”
ESM Graphs Supported
Another highlight of this update: It includes require()ing ESM graphs, he said. ECMAScript Modules (ESM) is a newer JavaScript module system than CommonJS modules.
“We have also included on this release several minor updates, which is requiring ESM modules from CommonJS modules,” he added. “It’s an experimental feature so far. But basically, you can now, with Node.js 22, require ESM modules inside a CommonJS module.”
That change is to address the fact that developers were having to create workarounds to import ESM modules, he added.
“With this pull request, you can pass this flag –experimental require module and then you can require that npm package and make it happen,” he said. “It’s also important to note it’s still experimental, so changes might happen.”
A New Default WebSocket Client
This update also includes a new WebSocket client, which has existed since Node.js 20 or 21, he added but was behind a flag. Now with Node.js, the WebSocket client is enabled by default. Previously, developers had to install a new package and rely on ws module for the client to have a WebSocket. With the new package, it just works.
“It was a ton of work,” he said. “With this new client, you can just require it. You don’t need to require globally installed, so you can just use it.”
It’s also maintained by Node.js, which has a more constrained environment, he added. In the past, if a developer added a module, it might have dependencies that needed to run that could impact an application.
“I would say that we reduce the attack vector in terms of supply chain attacks,” he said. “We also increased the maintainability since it’s a global WebSocket client, so any user in the community can maintain that instead of maintaining or relying on [a] different package to perform the same operation.”
None of the updates should create breaks in existing Node.js applications for regular users, he said, adding that breakage might be possible if you were using Node.js in a constrained environment where memory is important.
There are a few edge cases that it might impact, but considering this is only enabled through a flag, it shouldn’t cause any breakage, he said.
Another minor, experimental change that Gonzaga pointed out is a new experimental flag --run, which will execute scripts directly from the package JSON, he said. With this pull request, instead of using npm run test, developers can use Node --run test, which should be faster, he added.
He also reminded users that if they’re still running version 18, now is a good time to upgrade since Node is not releasing features for version 18, although they still will issue security patches.
“My strong recommendation is to jump to Node.js 20 and prepare to jump to Node.js 22 as soon as possible,” Gonzaga said.
TRENDING STORIES
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube channel to stream all our podcasts, interviews, demos, and more.
Loraine Lawson is a veteran technology reporter who has covered technology issues from data integration to security for 25 years. Before joining The New Stack, she served as the editor of the banking technology site Bank Automation News. She has...