Module
In one line
A module is a self-contained unit of code that can be imported and reused by other parts of a program.
In simple words
A module is a file (or group of files) that contains related code — functions, classes, variables — organized together and made available to other files through imports. Modules help you break a large program into manageable pieces.
Instead of writing everything in one giant file, you split your code into modules: a math module for calculations, a ui module for interface, a database module for data access. Each module has a clear responsibility and can be developed and tested independently.
Modules also control visibility. You explicitly export the parts you want to share and keep everything else private. This prevents naming collisions and makes dependencies explicit. JavaScript uses import/export, Python uses import, and Java uses package/import.
Example
// math.js
export function add(a, b) { return a + b; }
export function multiply(a, b) { return a * b; }
// main.js
import { add, multiply } from './math.js';
console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20The math module exports add and multiply. main.js imports and uses them. The module's internal details are hidden.
Related terms
Related Resources
Related Lessons
- Modules and Imports
Learn about modules and imports.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.