Modules — JavaScript

A module is a JavaScript file that shares some of its contents with other files. export shares a value from one file, and import takes it in another file. A…

Your app.js is 800 lines long. Price calculations, date formatting and button code are all mixed together in it. To find one function, you have to scroll up and down. To use the date formatting function in another project, you have to copy and paste it and hope nothing is left behind.

Modules are used to split code like that app.js into parts. A module is one JavaScript file that shares some of its contents with other files. The file that owns a function marks that function with export, and the file that needs it takes that function with import. This way each small file handles one thing.

Share with export, take with import

The example below has two files, each named in a comment. math.js shares PI and add, then app.js takes add:

// math.js
export const PI = 3.14

export function add(a, b) {
  return a + b
}

// app.js
import { add } from './math.js'

console.log(add(2, 3)) // 5

Need more than one value? Separate the names with commas: import { add, PI } from './math.js'.

Default export: one main value

Besides named exports, there is the default export. In this example math.js shares multiply as its default, and app.js takes it under the name times:

// math.js
export default function multiply(a, b) {
  return a * b
}

// app.js
import times from './math.js'

console.log(times(2, 3)) // 6

Be careful with this form: import add from './math.js', without curly braces. That form takes the default export, whatever its real name is. It does not take the named export add. If math.js only has named exports, that import is an error. The fix is to add curly braces: import { add } from './math.js'.

In short: a named export is imported with { } and exactly the same name. A default export is imported without { }, under any name you like.

Modules in the browser and in the exercises

In an HTML page, a module file is loaded with a <script> tag that has type="module":

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

import and export only work in a file that is loaded as a module, which is what type="module" does.

Other options. The rules for import depend on where your code runs:

Where The rule
A browser, with no extra tools The way used in this lesson. Write the full file address, with ./ and .js: './math.js'.
A Vite project or another bundler (a tool that joins module files) A name without ./, such as import { format } from 'date-fns', takes a package (ready-made code by other people) from the node_modules folder. npm install puts packages in that folder.
Node.js import works too. Write "type": "module" in package.json (the project's settings file) so that Node gives no warning. Older Node code uses require('./math.js') and module.exports, a system called CommonJS.

In this lesson's exercises, the editor treats your code as one module, so export works. The exercise check reads what you export, the way another file that imports your code would. The only thing that does not work in the exercises is import from another file, because an exercise has a single file. If you write import { add } from './math.js' in an exercise, you get the error Cannot find module './math.js'.

⚠️ Why can't the import find anything?

TL;DR: A module is a JavaScript file that shares some of its contents with other files. export shares a value from one file, and import takes it in another file. A file can have many named exports (export function add), and they are imported with curly braces and the exact name: import { add } from './math.js'. A file can have only one default export (export default ...), and it is imported without curly braces, under any name. In the browser, a module is loaded with <script type="module">.

⚡ export shares, import takes

Write:

// math.js
export function add(a, b) {
  return a + b
}

// app.js
import { add } from './math.js'

Meaning: A module is a JavaScript file that shares some of its contents with other files. export shares add from math.js, and import takes it in app.js. This is called a named export. A named export is imported with curly braces, and the name must match exactly: { Add } causes an error. './math.js' is the file's address, and ./ means in the same folder. A value that is not exported belongs only to its own file.

⚡ Default export: no curly braces

Write:

// math.js
export default function multiply(a, b) {
  return a * b
}

// app.js
import times from './math.js'
Guess first: export default marks one main value for a file. What does import add from './math.js', without curly braces, take?

The default export, whatever its real name is. It does not take the named export add. If the file has no default, that import is an error.

Meaning: A file can have only one default. The default is imported without { }, under any name you like, so times still takes multiply.

⚡ Modules in the browser

Write:

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

Meaning: type="module" loads app.js as a module. import and export only work in a file that is loaded as a module. An HTML file opened directly from your computer (a file:// address) cannot run modules, so open the page through a local server (a server on your own computer).

What you will learn