Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Modules and responsibility boundaries

ECMAScript modules separate internal declarations from explicit exports. Imports connect modules, while a barrel module collects re-exports behind one entry point.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

// Separate forms, not a single module.

// Export an existing local binding named `name`.
export { name };

// Import a named export as a local binding.
import { name } from "./module.mjs";

// Re-export a named export directly from another module.
export { name } from "./module.mjs";

An ECMAScript module has its own scope and runs in strict mode. export defines the interface available to other modules, while import exposes selected exports. A module boundary helps hide implementation details, but is not a security isolation boundary. Module syntax dates to ECMAScript 2015; loading and specifier resolution depend on the host environment.

Imported bindings are live: reading them reflects changes made in the exporting module. An importer cannot assign a new value to such a binding; this does not make an imported object immutable. Static import and export declarations occur at module top level.

A barrel module is a conventional name for a module that collects re-exports, not a separate language construct. In the example, index.mjs exposes increment and read, while count remains internal. Explicit re-exports document the public interface. export * does not forward the default export, and name collisions can make an import ambiguous.

Save the example's three sections as separate files in one directory; do not concatenate them into one script. Run main.mjs in an ECMAScript-module-capable console host. The expected output is 0 followed by 1 on separate lines. The example was run in Node.js, and its output matched the expected output exactly. This does not establish browser API support.

Examples

// counter.mjs
let count = 0;
export function increment() {
  count += 1;
}
export function read() {
  return count;
}

// index.mjs
export { increment, read } from "./counter.mjs";

// main.mjs
import { increment, read } from "./index.mjs";
console.log(read());
increment();
console.log(read());

Result

0
1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources