1. What is a Custom Module?
A custom module is a user-defined module containing reusable code such as functions, objects, or classes. Other parts of a Node.js application can use that code.
Why use custom modules?
- Organize code across multiple files.
- Reuse functions, objects, or classes.
- Improve maintainability and readability.
- Make an application more modular.
Core idea
2. Creating a Custom Module
Start by creating a JavaScript file. This file becomes your custom module. Define the functions, classes, or objects that you want the module to contain.
Example: math-utils.js
The source document demonstrates add() and multiply().
// math-utils.js
"kw">function add(a, b) {
"kw">return a + b;
}
"kw">function multiply(a, b) {
"kw">return a * b;
}
3. Exporting Code
Defining a function is not enough. To make it available to another file, export it.
Using module.exports
// math-utils.js
"kw">function add(a, b) {
"kw">return a + b;
}
"kw">function multiply(a, b) {
"kw">return a * b;
}
module.exports = {
add,
multiply
};
Using exports
"kw">function add(a, b) {
"kw">return a + b;
}
"kw">function multiply(a, b) {
"kw">return a * b;
}
exports.add = add;
exports.multiply = multiply;
add and multiply, so another file can access them with require().4. Importing and Using a Custom Module
The source material uses require() to load the custom module into another JavaScript file.
// arthmetic.js
"kw">const mathUtils = "kw">require('./math-utils');
"kw">const sum = mathUtils.add(5, 3);
"kw">const product = mathUtils.multiply(5, 3);
console.log(`Sum: ${{sum}}, Product: ${{product}}`);
math-utils.js is the toolbox. arthmetic.js opens that toolbox using require('./math-utils') and uses its tools.5. Organizing Code into Multiple Modules
As an application becomes larger, separating functionality into different modules can improve clarity and maintainability. Each module should handle a specific functionality.
Module files
"kw">function add(a, b) {
"kw">return a + b;
}
module.exports = add;"kw">function multiply(a, b) {
"kw">return a * b;
}
module.exports = multiply;"kw">function subtract(a, b) {
"kw">return a - b;
}
module.exports = subtract;"kw">function divide(a, b) {
"kw">return a / b;
}
module.exports = divide;Using all modules
// arthmetic.js
"kw">const add = "kw">require('./addition');
"kw">const multiply = "kw">require('./multiplication');
"kw">const divide = "kw">require('./division');
"kw">const subtract = "kw">require('./subtraction');
console.log(`Addition: ${add(5, 3)}`);
console.log(`Multiplication: ${multiply(5, 3)}`);
console.log(`Division: ${divide(6, 3)}`);
console.log(`Subtraction: ${subtract(5, 3)}`);
6. Quick Comparison
| Part | What it does | Example from the material |
|---|---|---|
| Custom module | Stores reusable user-defined code. | math-utils.js |
| Export | Makes module code available to another file. | module.exports / exports |
| Import | Loads a custom module into another file. | require('./math-utils') |
| Multiple modules | Separates different functionality into focused files. | addition, multiplication, division, subtraction |
7. Quick Check π―
Q1. Why do we create custom modules?
Q2. Which function is used in the source material to load a custom module?
Q3. Which can be exported from a custom module?
8. One-Minute Revision π§
1. Define reusable code in a JavaScript file.
2. Export it with
module.exports or exports.3. Require the module from another file.
4. Use the exported functions, objects, or classes.