# Modules: import, export and CommonJS

*JavaScript Developer Study Guide · Objects, Functions, and Classes · Last updated October 16, 2026 · https://www.certifyforce.com/study-guide/javascript-developer/objects-functions-and-classes/modules-import-export-and-commonjs*

How to import and export from an ES module (named, default, namespace, aliases, re-exports and dynamic import), why imports are hoisted read-only live bindings evaluated once, what script type=module changes, and how CommonJS require/module.exports and Node's module-type rules differ.

- Certification: Salesforce JavaScript Developer
- Exam section: Objects, Functions, and Classes
- Study guide: https://www.certifyforce.com/study-guide/javascript-developer

**Official exam objective:**

> Given a JavaScript module, give examples of how to use the module.

## What the exam expects

- Given a module's exports, write the import that reaches them: default, named, renamed with `as`, namespace (`* as`), or a mix.
- Spot invalid `export` and `import` syntax, and fix a barrel file that re-exports.
- Predict output that depends on live bindings, read-only imports, hoisted imports and the evaluate-once rule.
- Load a module on demand with `import()` and explain what `<script type="module">` changes.
- Use CommonJS `require`/`module.exports` correctly and decide whether Node treats a file as ESM or CommonJS.
- Key terms: **named export** (exported under its own name; imported in braces); **default export** (one per module; imported without braces under any name); **namespace object** (`import * as ns`; one property per export); **live binding** (an import that always shows the exporter's current value); **module specifier** (the string after `from`).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| Default exports | One per module; imported without braces under any name | [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) |
| Default plus named | `import def, { a } from` with the default first | [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) |
| Namespace object | Keys are export names; default is under `default` | [import()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) |
| `export *` | Re-exports named exports, not the default | [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) |
| Imported bindings | Live and read-only; object properties stay mutable | [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) |
| Import placement | Top level only; hoisted above the module's code | [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) |
| `import()` | Returns a promise for the namespace object | [import()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) |
| Module scripts | Deferred, strict, module-scoped, fail from `file://` | [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) |
| Top-level `this` in a module | `undefined` | [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) |
| Reassigning `exports` | Breaks the link to `module.exports` | [Modules: CommonJS modules (Node.js)](https://nodejs.org/api/modules.html) |
| `require` cache | Same object on every call; code runs once | [Modules: CommonJS modules (Node.js)](https://nodejs.org/api/modules.html) |
| `.js` with no `"type"` | Run as CommonJS; retried as ESM only if ES module syntax is found | [Modules: Packages (Node.js)](https://nodejs.org/api/packages.html) |
| ESM relative specifiers | File extension mandatory | [Modules: ECMAScript modules (Node.js)](https://nodejs.org/api/esm.html) |

## Check yourself

- Why does `typeof Pricing.basePrice` log `undefined` when `basePrice` is the default export?
- When should you use `import()` instead of a static import?
- Why does a top-level `var` in a module script not appear on `window`?
- What does `require('./x')` return the second time it is called?
- How does Node treat a `.js` file whose nearest package.json has `"type": "module"`?

## Sources

- [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
- [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)
- [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
- [import()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import)
- [Modules: CommonJS modules (Node.js)](https://nodejs.org/api/modules.html)
- [Modules: ECMAScript modules (Node.js)](https://nodejs.org/api/esm.html)
- [Modules: Packages (Node.js)](https://nodejs.org/api/packages.html)
- [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 13 linked practice questions, and unlimited timed mock exams are included with the JavaScript Developer pack (USD $19): https://www.certifyforce.com/practice-exams
