First Principles
Programming

ES Module System in Modern JavaScript

How import and export work, how ES Modules differ from CommonJS, and dynamic imports for loading code on demand.

By Fadjar Rafi. Published 20 Apr 2025. 2 min read.

Contents
  1. Export and import
  2. How it differs from CommonJS
  3. In the browser
  4. Dynamic imports

ES Modules (ESM) are JavaScript's built-in way to split code across files and wire them back together. It landed with ECMAScript 2015 (ES6) and is the standard the whole modern ecosystem is built on.

Before ESM, there was no official answer, so the community rolled its own — IIFEs, CommonJS (Node.js), AMD (RequireJS), UMD. ESM replaced that patchwork with one syntax that runs natively in browsers and Node.

Export and import#

You mark what a file shares with export, and pull it into another file with import.

// utils.js
export function sum(a, b) {
  return a + b
}

export const PI = 3.14159

export default function greet(name) {
  return `Hello, ${name}!`
}

There are two kinds: named exports (as many per file as you like) and one default export per file.

utils.js exports sum, PI, and greet; app.js imports them

The import side mirrors it:

// app.js
import greet from './utils.js' // default
import { sum, PI } from './utils.js' // named
import { sum as addNumbers } from './utils.js' // aliased
import * as Utils from './utils.js' // everything, as a namespace

console.log(greet('World')) // "Hello, World!"
console.log(sum(2, 3)) // 5
console.log(Utils.PI) // 3.14159

How it differs from CommonJS#

If you've worked in Node's CommonJS, the syntax is close but the model isn't:

// CommonJS
const { sum } = require('./utils.js')
module.exports = { myFunction }

// ES Module
import { sum } from './utils.js'
export const myFunction = () => {}

The key difference is that ESM's import/export are static — fixed at the top of the file and analyzed before the code runs. CommonJS's require() is dynamic and resolved at runtime. That static structure is what lets bundlers do tree shaking: because they can see exactly what's imported, they drop the exports nobody uses.

In the browser#

Add type="module" to a <script> and the browser treats it as ESM:

<script type="module">
  import { sum } from './utils.js'
  console.log(sum(1, 2))
</script>

<script type="module" src="app.js"></script>

A few things come with that: module scripts run in strict mode, execute asynchronously, are fetched with CORS, and need a real server — file:// won't work.

Dynamic imports#

import also works as a function that returns a promise, so you can load a module only when it's actually needed:

button.addEventListener('click', async () => {
  const { renderChart } = await import('./chart.js')
  renderChart(data)
})

Here chart.js isn't downloaded until the button is clicked — the basis of code splitting and lazy loading, which keep the initial page load small.

That's the core of it. Every modern framework and build tool — React, Vue, Vite, Rollup — is built on these same import/export foundations, so it's worth getting comfortable with them.