ES6 modules (also called ECMAScript modules or ESM) let you split JavaScript into separate files, where each file keeps its own scope and shares only what it marks with export. Another file brings those values in with import. This gives you organised code, no accidental globals and clear dependencies, and it is the standard module system in browsers and in current Node.js.
This guide explains es6 modules import export: named and default exports, import syntax, using modules in the browser, dynamic imports and the mistakes that cause errors.
Before modules, scripts shared one global scope, so two files could overwrite each other's variables and you had to load them in the right order by hand. A module is a file with its own top-level scope. Nothing leaks out unless you export it, and imports make the dependencies explicit, so tools and browsers know what loads what.
Modules also run in strict mode automatically, are evaluated only once even if many files import them, and are deferred, so they run after the HTML has been parsed.
A named export shares a specific value under its name. A file can have many.
The names in braces must match the exported names. You can rename on import with as, and import everything as one object with a namespace import:
You can also gather exports at the bottom of a file with export { add, multiply };, which many developers find easier to read.
A file can have one default export, which is the main thing the module provides. The importer chooses any name for it, without braces.
You can combine both kinds: import log, { LEVELS } from "./logger.js";.
| Feature | Named export | Default export |
|---|---|---|
| Count per file | Many | One |
| Import syntax | import { name } from | import anyName from |
| Name must match | Yes (or rename with as) | No, importer picks the name |
| Tooling and refactoring | Easier, names are consistent | Names can drift between files |
Many style guides prefer named exports because editors can auto-import them and renames stay consistent.
Add type="module" to the script tag. Paths must be real URLs or relative paths with the file extension.
Two things to remember. First, modules are fetched with HTTP and follow CORS rules, so opening the page with a file:// address usually fails; run a local server instead, for example python3 -m http.server. Second, a bare name such as import x from "lodash" works only with a bundler or an import map, because the browser needs a full path or URL.
import() loads a module on demand and returns a promise, which is useful for code splitting and rarely used features.
The module downloads only when the user clicks, which keeps the first page load small.
Imports are also live bindings: they are read-only views of the exporting module's variable. If the exporter changes a let variable, importers see the new value, but an importer cannot assign to the import. Circular imports can work but often lead to undefined values or a ReferenceError during start-up, so restructure shared code into a third module if you see that.
Node.js treats a file as an ES module when it ends in .mjs or when the nearest package.json has "type": "module". Files ending in .cjs, or projects without that setting, use the older CommonJS system with require and module.exports. You can use import of a CommonJS package from an ES module, but not the reverse with a plain require. Bundlers such as Vite and esbuild read your import statements, combine files and remove unused exports (tree shaking) for production.
ES modules use static import and export and load asynchronously, so tools can analyse them. CommonJS uses require and module.exports and loads synchronously at run time.
The file is being run as a classic script. Add type="module" in HTML, or set "type": "module" in package.json or use the .mjs extension in Node.js.
Yes, one default plus any number of named exports in the same file.
.js extension in imports?In browsers and in Node.js ES modules, yes. Bundlers sometimes let you leave it out, but writing it is safest.
ES modules give every file its own scope and share values with export and import. Use named exports for many values and a default export for one main value, load modules with type="module" from a web server, and use import() to load code only when needed. Keep dependencies simple and avoid circular imports.