ESM: ECMAScript Modules

Learn about ECMAScript modules and how to use ESM in Node.js with examples.

We'll cover the following

ECMAScript modules (also known as ES modules or ESM) were introduced as part of the ECMAScript 2015 specification with the goal to give JavaScript an official module system suitable for different execution environments. The ESM specification tries to retain some good ideas from previous existing module systems like CommonJS and AMD. The syntax is very simple and compact. There’s support for cyclic dependencies and the possibility to load modules asynchronously.

The most important differentiator between ESM and CommonJS is that ES modules are static, which means that imports are described at the top level of every module and outside any control flow statement. Also, the names of the imported modules cannot be dynamically generated at runtime using expressions; only constant strings are allowed.

For instance, the following code won’t be valid when using ES modules:

Get hands-on with 1200+ tech skills courses.