JavaScript Modules

In the previous lesson, we learned how to give function parameters fallback values. Now let’s step back and look at how to organize larger projects by splitting code across multiple files using modules.

πŸ“¦ What is a Module?

A module is simply a JavaScript file that holds part of your code. Instead of writing everything in one huge file, you break the code into smaller files, and each file is a module that focuses on one job.

Each module can share its code with other modules. One file exports the pieces it wants to make available, and another file imports them. We will cover that export and import syntax in depth in the next lesson, so here we focus on the idea behind modules and why they matter.

Here we split a tiny program across two files and let one use the other:

// math.js β€” a module that does math
export function add(a, b) {
return a + b;
}
// app.js β€” a module that uses the math module
import { add } from "./math.js";
console.log(add(2, 3)); // 5

Let’s walk through what each file does:

  • export function add(a, b) in math.js defines the add function and marks it with export, which makes it available to other files.
  • import { add } from "./math.js" in app.js pulls that exact function in from the math.js file.
  • console.log(add(2, 3)) then calls the imported function and prints 5, even though add was written in a different file.

πŸ€” Why Modules Exist

As a project grows, putting all your code in one file becomes hard to read and hard to change. Modules let you keep each part of the project in its own file, so the code stays organized and easy to find.

Benefit What it means
Avoid giant files Each file stays small and focused on one task
Reuse code Write a helper once and import it wherever you need it
Avoid naming collisions Variables in one module do not clash with variables in another

πŸ”’ Each Module Has Its Own Scope

Inside a module, the variables and functions you create are private to that file. They do not leak out into other files or into the page, which means two modules can use the same variable name without any conflict.

Here two separate files each declare a variable with the very same name:

user.js
const name = "Alex";
// product.js
const name = "Laptop"; // βœ… no conflict β€” this name belongs only to product.js

Let’s see why these two lines never clash:

  • const name = "Alex" lives inside user.js, so that name belongs only to user.js.
  • const name = "Laptop" lives inside product.js, so that name belongs only to product.js.
  • Because each module keeps its own scope, the two name variables stay completely separate and neither overwrites the other.

This is different from old scripts, where every variable lived in one shared global space and could overwrite another. With modules, the only things shared are the ones you choose to export.

Sharing is on purpose

Module scope means nothing is shared by accident. If you want to use a value from another file, you export it and import it. Everything else stays private.

🌐 Turning On Modules in the Browser

To use a file as a module in the browser, you add type="module" to the script tag. This tells the browser to treat the file as a module and allows it to use import and export.

This single script tag is what switches a file into module mode:

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

Let’s break down this tag:

  • src="app.js" points the browser at the file we want to load.
  • type="module" tells the browser to run that file as a module instead of an ordinary script.
  • With that attribute in place, app.js is now allowed to use import and export.

Without type="module", the browser treats the file as an ordinary script, and import or export will cause an error.

Modules need a server

Modules are loaded over the network, so opening the file directly with a file:// path usually fails. Run a local server (for example, the Live Server extension) and open the page through http://localhost.

⚠️ Common Mistakes to Avoid

Mistake Problem Solution
Forgetting type="module" The browser rejects import and export with an error Add type="module" to the script tag
Opening the file with file:// The module fails to load because of browser security rules Serve the page through a local server
Expecting global variables to be shared A variable in one module is not visible in another Export the value and import it where you need it

πŸ”§ Try It Yourself!

  1. Create a file named math.js and put a small helper function inside it.
  2. Create a second file named app.js that will use that helper.
  3. Picture how app.js would bring in the function from math.js rather than copying it.
  4. Add <script type="module" src="app.js"></script> to an HTML page.
  5. Open the page through a local server and confirm it loads without errors.

🧩 What You’ve Learned

  • βœ… A module is a single file that holds part of your code
  • βœ… Modules keep large projects organized and let you reuse code
  • βœ… Each module has its own scope, so variable names never collide
  • βœ… You add type="module" to the script tag to enable modules in the browser
  • βœ… Modules need a local server, not a file:// path

πŸš€ What’s Next?

Now that you understand why modules exist, we will learn the exact syntax for sharing code between files. Let’s continue to Import and Export.

Share & Connect