Lesson 5 / 28

ES Modules in Node

The standard import/export syntax, natively supported.

import / export

Set "type": "module" in package.json, or use a .mjs extension, to use ES module syntax.

// math.mjs
export function add(a, b) {
  return a + b;
}

// app.mjs
import { add } from "./math.mjs";
console.log(add(2, 3));

Output:

5

CommonJS vs ESM

ESM imports are resolved statically and support top-level await; CommonJS require calls are dynamic and synchronous.