Node.js β€’ Unit 3 β€’ Custom Modules

Custom Modules

Learn how to create your own reusable Node.js modules, export functions, and use them from other files.

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.

πŸ§’ ELI10: Think of a module as a small toolbox. You put useful tools inside it, label the toolbox, and another program can take the tools it needs.

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

Custom Module→ Define code→ Export→ require()→ Reuse

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.

1
Create a file.
Example: math-utils.js
2
Define reusable functions.
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;
✍️ Remember: Both approaches export 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}}`);
🧠 Visualize it: 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.

math application β”œβ”€β”€ addition.js β†’ add() β”œβ”€β”€ multiplication.js β†’ multiply() β”œβ”€β”€ division.js β†’ divide() β”œβ”€β”€ subtraction.js β†’ subtract() └── arithmetic.js β†’ loads and uses the modules

Module files

addition.js
"kw">function add(a, b) {
  "kw">return a + b;
}

module.exports = add;
multiplication.js
"kw">function multiply(a, b) {
  "kw">return a * b;
}

module.exports = multiply;
subtraction.js
"kw">function subtract(a, b) {
  "kw">return a - b;
}

module.exports = subtract;
division.js
"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

PartWhat it doesExample from the material
Custom moduleStores reusable user-defined code.math-utils.js
ExportMakes module code available to another file.module.exports / exports
ImportLoads a custom module into another file.require('./math-utils')
Multiple modulesSeparates different functionality into focused files.addition, multiplication, division, subtraction

7. Quick Check 🎯

Q1. Why do we create custom modules?

βœ” Correct: Custom modules help organize and reuse code.

Q2. Which function is used in the source material to load a custom module?

βœ” Correct: require()

Q3. Which can be exported from a custom module?

βœ” Correct: Functions, objects, or classes can be exported.

8. One-Minute Revision 🧠

Define β†’ Export β†’ Require β†’ Use

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.