The adoption of ECMAScript Modules (ESM) for development has become mainstream, yet Node.js continues to use the traditional CommonJS (CJS) format. Th

Native Support for CJS/ESM Interoperability Begins in Node.js 22

submited by
Style Pass
2024-04-29 07:30:04

The adoption of ECMAScript Modules (ESM) for development has become mainstream, yet Node.js continues to use the traditional CommonJS (CJS) format. This has posed challenges for library and application developers. However, with the recent release of version 22, Node.js has begun to experimentally support ESM natively, and this support is expected to stabilize in future versions. Although current support has its limitations and is not yet perfect, it is sufficient to improve the existing situation. This article will provide a detailed overview.

ECMAScript Modules (ESM) are an official standard for structuring and loading modules in JavaScript. Unlike CJS, which executes synchronously, ESM was designed with the capability to load asynchronously. This design supports top-level await commands, allowing modules to handle asynchronous operations before they are used.

Despite this capability, ESM can also be executed synchronously if the module graph (the structure consisting of all the modules and their dependencies) does not contain any top-level await. 

Leave a Comment