BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Node.js 22 Released with Increased Support for ESM Modules and Web APIs

Node.js 22 Released with Increased Support for ESM Modules and Web APIs

This item in japanese

The Node.js team recently released Node.js 22. Key features include increased compatibility with JavaScript native modules and web APIs, together with performance improvements.

Node.js 21 already supported key JavaScript module APIs, among which the import.meta object. Concretely, Node.js 21 supports both import.meta.dirname and import.meta.filename. Node.js 22 adds support for synchronous require of JavaScript module (ESM) graphs. The release note details:

If --experimental-require-module is enabled, and the ECMAScript module being loaded by require() meets the following requirements:

  • Explicitly marked as an ES module with a “type”: “module” field in the closest package.json or a .mjs extension.
  • Fully synchronous (contains no top-level await).

require() will load the requested module as an ES Module, and return the module name space object. In this case, it is similar to dynamic import() but is run synchronously and returns the name space object directly. We intend to eventually enable require(esm) by default in the future, without the flag.

Tech blogger CoderOasis provided a simple code sample illustrating the feature:

// ES module: math.js  
export function add(a, b) {  
return a + b;  
}  
  
// CommonJS module: app.js  
const { add } = require('./math.js');  
console.log(add(2, 3)); // Output: 5  
  

Node.js 21 already featured partial support for the Navigator API and experimental support for a browser-compatible Websocket client. In Node.js 22, Websocket support remains experimental but is now the default: developers no longer need to use a flag to activate it.

Node.js 22 shows continued progress on the performance front.

Node.js 22 updates the V8 JavaScript engine to 12.4, which leverages an optimizing JIT compiler called Maglev (released between the existing Sparkplug and Turbofan compilers). The 12.4 update includes features such as WebAssembly Garbage Collection and Iterator Helpers. The performance of Node.js streams also improved through the increase of the High Water Mark for streams from 16KiB to 64KiB. As this slightly increases memory usage, for memory-sensitive environments the default value can be adjusted via setDefaultHighWaterMark(). The performance of the fetch() API and test runner has been boosted by improving the efficiency of AbortSignal instance creation.

Node.js is open-source software available under the MIT license from the Open JS Foundation. Contributions and feedback are encouraged via the Node.js contribution guidelines and code of conduct.

About the Author

Rate this Article

Adoption
Style

BT