Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 27 / 34

Modules: ESM & CommonJS

01

Understand the idea

A file works alone but fails when copied into a browser: require is not defined. Its belongs to a different runtime setup.

A public interface for a file

As an application grows, separate files need to collaborate without every temporary name becoming global. A module makes the relationship explicit: this file provides a calculation, and that file imports it. Reading the imports gives you a first map of a file's dependencies.

Named and default exports

ES modules use import and export. Named exports must be imported by their exported name unless aliased; a default export has its own import form. Static imports belong at module top level, while dynamic import returns a and can load code on demand.

An export marks a value as part of the file's public interface. A named import uses the exported name, possibly with an alias. A default export has one default slot that the importing file can name locally. The braces in a named import select named exports; they are not destructuring of a default export.

ES modules and CommonJS side by side
FeatureES modulesCommonJS
Exportexport / export defaultmodule.exports / exports properties
LoadStatic import or dynamic import()require()
Normal browser supportYes, with module scripts or dynamic importNeeds tooling or a compatible runtime
Binding behaviorNamed imports are live, read-only bindingsRequire returns the exported value/object
Execution timingStatic dependencies are linked before evaluationRequire loads during execution
Choosing a systemFollow the environment and project configurationDo not mix syntax without checking interoperability

ES modules in the browser

Module keeps top-level declarations out of the global scope. ESM runs in , and top-level this is . Imported bindings are live views of exported bindings, but importers cannot reassign them directly. A module normally evaluates once per module in a given runtime, so its top-level state can be shared by its importers.

CommonJS is a different module system

CommonJS uses require and module.exports and remains common in Node.js code. How Node treats a .js file depends on package configuration; .mjs and .cjs make the intended format explicit. Interoperability has rules, so do not assume a default import and a destructured require always have the same shape.

CommonJS uses require and module.exports, usually in Node.js projects. ES modules use import and export and are supported by modern browsers as modules. File extensions and a package's type setting matter in Node. If a copied snippet says require is not defined in a browser, first check whether you copied code for the wrong module environment.

Follow a dependency without leaking globals

In a browser, ESM needs a module script, valid module URLs, and appropriate server responses. Bare package names usually need a bundler or import map. The worked example dynamically imports a tiny ESM data URL so its exported interface is visible without pretending the browser provides CommonJS.

The example imports a tiny module from a data URL so the sandbox can demonstrate an actual module without extra files. A normal application would import from a file URL or a path resolved by its build tool. Dynamic import returns a promise for a module namespace object, whose exported addTax the example then calls.

02

Mental model

FOLLOW THE VALUE

module source
export interface
addTax

Only the exported binding is public.

01 / 03Trace the transformation

03

Live example

The exported source is deliberately one line. import() resolves to a namespace , and addTax is its named exported .

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Use the exported function

Call the named export with 10 instead of logging the function itself.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

11

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does dynamic import return?
    02Does an ESM top-level const automatically become window.constName?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson