Everything, Everywhere
Verified Specification | Standardized Formulas | Instant Precision
Secure & Private (Zero Data Retention) Free Access • No Sign-Up

JavaScript Modules

Modules allow you to break large codebases into independent, reusable files.

Try It Yourself

      
📋 Copy ES Modules Export and Import Cheat Sheet
// ES Modules (ESM) Standard Syntax
// math.js:
export const PI = 3.14159;
export function add(a, b) { return a + b; }
export default class Calculator {}

// app.js:
import Calculator, { PI, add } from "./math.js";

⚠️ 5 Fatal Traps & JavaScript Pitfalls

Trap #1: Missing File Extension in Browser ESM
In standard browser ES modules, writing import { add } from "./math" fails with a 404 error. Browsers require the full file extension: "./math.js".
Trap #2: CommonJS vs. ES Module Interop Confusion
Mixing require() and import statements in the same file causes syntax errors. Configure "type": "module" in package.json to standardize on ESM.
Trap #3: Default Export Renaming Confusion
Default exports can be imported under any arbitrary name, which can lead to inconsistent naming across team files. Prefer named exports for refactoring clarity.
Trap #4: Dynamic import() Returns a Promise
import(modulePath) is asynchronous and returns a Promise. Forgetting to await import(...) leaves you with a pending promise instead of module exports.
Trap #5: Top-Level Await Blocking Module Tree Execution
Using top-level await in an imported module pauses the execution of all parent importing modules until the promise resolves.

💬 Frequently Asked Questions

What is the difference between named exports and default exports?
Named exports allow multiple exports per file imported using matching names in braces { a, b }. Default export allows a single primary export imported without braces.
What is dynamic import() and when should you use it?
dynamic import("./module.js") loads modules on-demand at runtime, enabling code-splitting and conditional loading of heavy dependencies.
How do you tell Node.js to use ES Modules?
Set "type": "module" in your project package.json file or use the .mjs file extension.
Are ES modules executed in strict mode by default?
Yes. All ES modules are automatically evaluated in strict mode ("use strict") without needing the directive.
What does import.meta provide in ES modules?
import.meta contains contextual metadata about the current module, such as import.meta.url (the file URL of the running module).
Sponsored Utility
While You're Here
Sponsored Recommendations
Advertisement