Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. One of the key features of Node.js is its module system, which allows developers to break up their code into smaller, reusable pieces called modules. In this tutorial, we will explore how to create and use modules in Node.js.
What is a Module?
A module in Node.js is essentially a JavaScript file that encapsulates a set of related functions, variables, and classes. Modules allow developers to organize their code in a structured way, making it easier to manage and maintain.
Creating a Module
To create a module in Node.js, simply create a new JavaScript file and write the code you want to encapsulate within that file. For example, let’s create a module that defines a simple function for adding two numbers.
- Create a new file called ‘math.js’ and add the following code:
//math.js
function add(a, b) {
return a + b;
}
module.exports = {
add: add
};
In this code snippet, we define a function add
that takes two parameters a
and b
and returns their sum. We then use the module.exports
object to make the add
function available for use in other modules.
Using a Module
Once you have created a module, you can use it in other modules by requiring it using the require
function. Let’s create a new file called ‘app.js’ and use the ‘math’ module we created earlier.
- Create a new file called ‘app.js’ and add the following code:
//app.js
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5
In this code snippet, we use the require
function to import the ‘math’ module. We then call the add
function from the ‘math’ module and pass in two numbers (2 and 3) as arguments. The sum of the two numbers (5) is then logged to the console.
Built-in Modules
In addition to creating custom modules, Node.js also provides a set of built-in modules that can be used to extend the functionality of your applications. Some commonly used built-in modules include ‘fs’ for file system operations, ‘http’ for creating HTTP servers, and ‘path’ for working with file paths.
To use a built-in module, simply require it in your code like any other module. For example, to create an HTTP server using the ‘http’ module:
//server.js
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
console.log('Server running at http://localhost:8080/');
In this code snippet, we require the ‘http’ module and use it to create an HTTP server that listens on port 8080. When a request is received, the server responds with the message ‘Hello World!’ and logs a message to the console.
Conclusion
Modules are an essential feature of Node.js that allow developers to break up their code into smaller, reusable pieces. By creating custom modules and using built-in modules, developers can build modular and scalable applications in Node.js. This tutorial covered the basics of creating and using modules in Node.js, but there is much more to learn about modules and how to leverage them effectively in your code. Happy coding!
Well explained..
thank u sir😊
Wow, amazing video sir, i don't know why other youtubers are getting more views but you explained better than all of them. Thank you sir.
best explanation. simple and clear. thanks alot
Very good explaination atleast better than chai coffee , Those who know know
11:27 "module.exports sirf ek hi bar use hota hai" se ye matlab tha ki
we can just do module.exports = { func1, func2,….. };
not like module .exports = { }; then another module.exports = { }; in next line?
🎯 Key points for quick navigation:
00:38 🧩 Modular programming divides the code base into smaller modules for better organization.
01:43 📁 Creating different files for different functions/modules helps in better code maintenance.
02:24 📥 Use the "require" function to import modules in Node.js, similar to importing packages in other programming languages.
05:24 📦 Value is being exported from the module.exports
05:40 🛠️ Function add is working fine
05:52 🧮 Addition function is exported
06:11 🧩 Understanding the approach for functions
06:30 🧠 Correcting function errors
07:03 💡 Debugging and resolving value overrides
07:47 🚀 Using destructuring for clean code
08:16 📁 Exporting functions in multiple ways
09:26 📦 Utilizing exports to manage importing and exporting data
10:09 🔁 Implementing various ways to export functions
10:37 📦 Functions are properties and can be named freely
11:07 🎛️ Use exports to expose functions and values in Node.js
12:31 📁 Understanding file system in Node.js for file handling
13:44 🔒 Cryptography modules in Node.js provide powerful tools for encryption and hashing
14:55 📦 Modular programming in Node.js involves requiring packages and making the program modular
Made with HARPA AI
crystall and clear
Great video sir 🙇♂🙏✨💖
Truly your nodeJS series content is unbeatable. Keep up the good work Piyush. May your channel grow by leaps and bounds!! Bcz you truly deserve it.
Best Teaching 👌
Thank you Bhayya
Beautiful explanation! Came here to revise NodeJS, but learned a lot of new things. Going to watch the whole playlist now!
Very helpful video sir❤❤❤
Thank you sir🎉🎉
Started this series on 22-06-2024. Now let's see in how many days i can understand the node.js and complete the series.
I love the way you explain.
Really Like your Video
bro u have got tier teaching
😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😃😃😃😃😃😃😃😃😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍🤩🤩🤩🤩🤩🤩🤔🤔😎😎😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😘😘😘😘😘😘🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰😍😍😍😍😍😍😍😍😎😎😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😘😘🥰🥰🥰🥰😘😘🥰🥰😘😘😍😍😘😘🥰🥰😘😘😍😍😘😘🥰🥰🥰🥰😘😘😍😍😘😘😘😘😍😍😘😘😘😘😍😍🥰🥰🥰🥰😍😍🥰🥰🥰🥰🥰🥰
sir most underrated channel hope you will achieve more and more in future
bretter than million of paid courses
I think you are better and way so much good more than codewithharry and Apnacollage good sir really appreciated