
Node.js Tutorial – Day 2: Understanding Node.js Modules
Welcome back to our Node.js Tutorial series! In the previous lesson, we learned about the introduction to Node.js, its features, installation, and built our first program.
Today, we are diving into one of the most important concepts in Node.js: Modules. Understanding modules will help you organize, reuse, and maintain your code effectively.
What Are Modules in Node.js?
A module in Node.js is simply a block of code (functions, objects, or variables) that can be reused across files. Node.js applications are built on a modular architecture, meaning you can break large projects into smaller, manageable files.
Node.js comes with:
- Core Modules → Built-in, provided by Node.js.
- Local Modules → Custom modules created by developers.
- Third-Party Modules → Installed via NPM.
Core Modules in Node.js
Core modules are built into Node.js and can be used without installation. Some popular ones include:
- fs → File System operations
- http → Creating servers and handling requests
- path → Working with file and directory paths
- os → System-related information
- url → Parsing and formatting URLs
Example: Using the fs
module
const fs = require('fs');
// Writing to a file
fs.writeFileSync('example.txt', 'Hello from Node.js Tutorial!');
// Reading from a file
const data = fs.readFileSync('example.txt', 'utf8');
console.log(data);
Output:
Hello from Node.js Tutorial!
Creating Your Own Module
You can also create your own module in Node.js by using module.exports
.
Example: Custom Math Module
math.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };
app.js
const math = require('./math');
console.log("Addition:", math.add(5, 3));
console.log("Subtraction:", math.subtract(10, 4));
Output:
Addition: 8
Subtraction: 6
This is a simple way to separate logic into reusable files.
Using Third-Party Modules with NPM
Node.js has a massive ecosystem of packages available through NPM (Node Package Manager).
Example: Using the chalk
package
npm install chalk
app.js
const chalk = require('chalk');
console.log(chalk.green("Success!"));
console.log(chalk.red("Error!"));
Output:
- “Success!” in green
- “Error!” in red
This shows how easily you can add new functionality to your project.
CommonJS vs ES Modules
By default, Node.js uses CommonJS (require
, module.exports
).
But you can also use ES Modules (ESM) with import
and export
.
Example with ES Modules
math.mjs
export function multiply(a, b) {
return a * b;
}
app.mjs
import { multiply } from './math.mjs';
console.log("Multiplication:", multiply(4, 5));
Run using:
node app.mjs
This syntax is closer to modern JavaScript (ES6+).
Mini-Project: Building a Utility Module
Let’s create a small utility module that handles temperature conversions.
converter.js
function celsiusToFahrenheit(celsius) {
return (celsius * 9/5) + 32;
}
function fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
module.exports = { celsiusToFahrenheit, fahrenheitToCelsius };
app.js
const converter = require('./converter');
console.log("25°C =", converter.celsiusToFahrenheit(25), "°F");
console.log("77°F =", converter.fahrenheitToCelsius(77).toFixed(2), "°C");
Output:
25°C = 77 °F
77°F = 25.00 °C
This mini-project demonstrates how to organize and export functions effectively.
Best Practices for Using Modules
- Keep modules small and focused (Single Responsibility Principle).
- Use meaningful file names (e.g.,
userController.js
,authService.js
). - Prefer ES Modules for modern projects.
- Always manage dependencies with package.json.
Conclusion
In this lesson of our Node.js Tutorial, you learned about:
- Core, local, and third-party modules
- How to use
require
andmodule.exports
- Difference between CommonJS and ES Modules
- A mini-project to practice creating your own modules
Modules are the foundation of clean and scalable Node.js applications.
Source: Node.js Modules Documentation
👉 Up next: Day 3 – Node.js Event Loop Explained