← Back to JavaScript, page 1

javascript-0016 · Medium mid

How do ES modules work in JavaScript? Explain named exports, default exports, and live bindings.

An ES module is a file with its own scope that shares values through export and receives them through import. A module can have many named exports and one default export. Imports are live read-only views of the exporting module's variables, and the module code runs only once, however many files import it.

Full answer

Without modules, every script shares one global scope, so names collide. An ES module is a separate file with its own top-level scope. Nothing leaves the file unless it is exported.

A named export attaches a name: export const PI = 3.14 or export function area() {}. The importing file must use the same name, for example import { PI, area } from './math.js', and may rename it with as. A default export is written as export default something. A module has at most one, and the importer picks any name for it: import describe from './math.js'. Many style guides prefer named exports because tools and editors can check the names.

Imports are not copies. They are live bindings: the importer sees the current value of the exported variable. If the exporting module changes export let visits, importers see the new number. The importer cannot assign to the import, though. Trying to do so throws a TypeError.

Modules are evaluated once and cached, in strict mode, and their imports are resolved before the code runs. Static import statements must be at the top level, which lets bundlers remove unused code (tree shaking). The import() function loads a module on demand and returns a promise, which is used for code splitting. Top-level await is allowed in modules.

CommonJS (require and module.exports) is the older Node.js format. It loads synchronously and exports a copy of the value at the time of the call.

javascript
// The module source is kept in a string so this file runs on its own.
// In a real project this text would be a separate file, math.js.
const mathSource = `
  export const PI = 3.14159;
  export function area(radius) { return PI * radius * radius; }
  export let visits = 0;
  export function visit() { visits += 1; }
  export default function describe() { return 'math module'; }
`;

const url = 'data:text/javascript,' + encodeURIComponent(mathSource);
const math = await import(url);

console.log(Object.keys(math));
console.log(math.default());
console.log(math.area(2));

console.log('visits before:', math.visits);
math.visit();
console.log('visits after:', math.visits);

try {
  math.visits = 10;
} catch (error) {
  console.log('assign to import:', error.name);
}

const again = await import(url);
console.log('same module instance:', again === math);

Run with Node.js v24.22.1 as js-16.mjs; two runs gave identical output. Output: [ 'PI', 'area', 'default', 'visit', 'visits' ] math module 12.56636 visits before: 0 visits after: 1 assign to import: TypeError same module instance: true

Key points

  • Each module has its own scope; only exported names are visible outside.
  • Named exports are imported by name; a module has at most one default export.
  • Imports are live, read-only bindings, not copies.
  • Modules run once and are cached; they are always in strict mode.
  • import() loads a module lazily and returns a promise.

Common mistakes

  • Mixing default and named import syntax, such as import { describe } from a file that only has a default export.
  • Expecting an import to be a copy and then wondering why it changes.
  • Creating a circular import and reading a binding before it is initialized.
  • Forgetting the file extension in Node.js ESM or the type setting in package.json.

Follow-ups

  • How do circular dependencies behave in ES modules compared with CommonJS?
  • What is tree shaking, and which module feature makes it possible?
  • How does dynamic import() help with page load performance?

Tips

  • Contrast ES modules with CommonJS in three points: syntax, loading time, and live versus copied values.
  • Mention strict mode and single evaluation; those are quick marks of depth.
  • Prefer named exports in your own examples and explain why in one sentence.