JavaScript Modules
Table of Contents + β
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 mathexport function add(a, b) { return a + b;}
// app.js β a module that uses the math moduleimport { add } from "./math.js";
console.log(add(2, 3)); // 5Letβs walk through what each file does:
export function add(a, b)inmath.jsdefines theaddfunction and marks it withexport, which makes it available to other files.import { add } from "./math.js"inapp.jspulls that exact function in from themath.jsfile.console.log(add(2, 3))then calls the imported function and prints5, even thoughaddwas 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:
const name = "Alex";
// product.jsconst name = "Laptop"; // β
no conflict β this name belongs only to product.jsLetβs see why these two lines never clash:
const name = "Alex"lives insideuser.js, so thatnamebelongs only touser.js.const name = "Laptop"lives insideproduct.js, so thatnamebelongs only toproduct.js.- Because each module keeps its own scope, the two
namevariables 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:
<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.jsis now allowed to useimportandexport.
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!
- Create a file named
math.jsand put a small helper function inside it. - Create a second file named
app.jsthat will use that helper. - Picture how
app.jswould bring in the function frommath.jsrather than copying it. - Add
<script type="module" src="app.js"></script>to an HTML page. - 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.