# ES Modules (ECMAScript Modules)

URL: https://softwaredictionary.org/terms/es-modules
Category: Web Development
Last updated: 2026-10-05
In Turkish: ES Modülleri
Pronunciation: ee-es MOJ-oolz

In short: ES modules are JavaScript's built-in module system: each file keeps its own scope and shares code with other files through import and export.

## What are ES modules?

A module is a file whose variables and functions stay private unless it exports them. With ES modules, a file marks what it shares with `export`, and another file pulls those pieces in with `import`. The standard arrived in ECMAScript 2015, also called ES6, and every modern browser and Node.js now support it.

Before ES modules, JavaScript had no module system of its own. Browsers loaded scripts that all shared one global scope, and Node.js used CommonJS, with `require()` and `module.exports`. ES modules differ in one important way: imports and exports are static, written at the top level, so tools can see the whole dependency graph before running anything. That is what lets bundlers drop unused code, known as tree shaking, and split the rest into chunks.

In the browser, a script becomes a module with `<script type="module">`; module scripts are deferred and always run in strict mode. In Node.js, a file is a module if it ends in `.mjs` or if the nearest `package.json` says `"type": "module"`. For code that should load only when it is needed, the `import()` function loads a module on demand and returns a promise.

## Key takeaways

- ES modules share code with `export` and `import`, and keep everything else private.
- They are part of the language standard and run in browsers and Node.js.
- Static imports let tools see the dependency graph, which makes tree shaking possible.
- `import()` loads a module only when it is needed.

## Example: Exporting from one file and importing into another

```javascript
// math.js
export const PI = 3.14159;
export function area(radius) {
  return PI * radius ** 2;
}

// app.js
import { area } from "./math.js";
console.log(area(2)); // 12.56636
```

## Frequently asked questions

**What is the difference between ES modules and CommonJS?**

CommonJS is Node.js's older module system: `require()` loads a module while the code runs, and `module.exports` shares values. ES modules use `import` and `export`, load asynchronously and are analyzed before the code runs. Node.js supports both, and new code usually uses ES modules.

**Why does the browser say "Cannot use import statement outside a module"?**

The file was loaded as a classic script. Add `type="module"` to its `<script>` tag; in Node.js, name the file `.mjs` or set `"type": "module"` in `package.json`.

## Sources

- [ECMAScript Language Specification: Modules](https://tc39.es/ecma262/#sec-modules)
- [MDN: JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
- [Node.js documentation: ECMAScript modules](https://nodejs.org/api/esm.html)

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
