Curriculum
- 11 Sections
- 35 Lessons
- 10 Weeks
- Introduction to Backend Development5
- JavaScript for Backend (Core Concepts)4
- Node.js Fundamentals4
- Building Server with Express.js4
- REST API Development4
- Database with MongoDB4
- Authentication & Security2
- Advanced Backend Concepts3
- Deployment & DevOps Basics1
- Real Project1
- Career & Interview Preparation3
Modules and require in Node.js
Modules and require in Node.js – Complete Beginner Guide
Modules in Node.js help you organize code into separate files. They make your code clean, reusable, and easy to manage. In backend development, modules are used in almost every project.
What are Modules in Node.js
A module in Node.js is a file that contains code. It can be functions, variables, or objects. You can reuse this code in other files using require.
Types of Modules in Node.js
Node.js supports three main types of modules.
Built-in modules are provided by Node.js such as fs and http.
User-defined modules are created by developers.
Third-party modules are installed using npm.
Creating a Module in Node.js
You can create your own module by writing code in a separate file.
function add(a, b) {
return a + b;
}
module.exports = add;
What is require in Node.js
The require function is used to import modules into another file. It allows you to use functions or data from other files.
Using require in Node.js
const add = require(‘./math’);
console.log(add(5, 3));
In this example, the add function is imported from math.js and used in app.js.
Exporting Multiple Functions
You can export multiple functions using an object.
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a – b;
}
module.exports = { add, subtract };
const utils = require(‘./utils’);
console.log(utils.add(10, 5));
console.log(utils.subtract(10, 5));
Built-in Modules Example
Node.js provides built-in modules like fs for file handling.
fs.writeFileSync(‘test.txt’, ‘Hello Node.js’);
Why Modules are Important
Modules help in organizing code. They improve readability and reusability. They also make large applications easier to manage.
Best Practices for Modules
Keep modules small and focused. Use clear file names. Export only required functions. Avoid large files.
Internal Link
Click here for more free courses
FAQs
What is a module in Node.js
A module is a file that contains reusable code.
What is require in Node.js
Require is used to import modules into another file.
What is module.exports
It is used to export functions or data from a module.
Can we use multiple modules
Yes, Node.js allows multiple modules in a project.



