Node.js modules

Posted by


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.

  1. 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.

  1. 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!

0 0 votes
Article Rating
35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sneha_06.
2 months ago

Well explained..
thank u sir😊

@AlokKumar-ru7us
2 months ago

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.

@arshiashahid8607
2 months ago

best explanation. simple and clear. thanks alot

@deeppanchal5102
2 months ago

Very good explaination atleast better than chai coffee , Those who know know

@Anku-b9
2 months ago

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?

@khushisinha3456
2 months ago

🎯 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

@MATH_VIBE
2 months ago

crystall and clear

@_hulk748
2 months ago

Great video sir 🙇‍♂🙏✨💖

@anushiagrawal
2 months ago

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.

@sufiyanpatel6241
2 months ago

Best Teaching 👌

@ArunKumar_237
2 months ago

Thank you Bhayya

@shiveshanand5812
2 months ago

Beautiful explanation! Came here to revise NodeJS, but learned a lot of new things. Going to watch the whole playlist now!

@santoshbhujbal5650
2 months ago

Very helpful video sir❤❤❤
Thank you sir🎉🎉

@tusharsingh1915
2 months ago

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.

@ShubhamG800
2 months ago

I love the way you explain.

@ahmedbaig8543
2 months ago

Really Like your Video

@aloratg3226
2 months ago

bro u have got tier teaching

@aboltabol77
2 months ago

😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😃😃😃😃😃😃😃😃😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍🤩🤩🤩🤩🤩🤩🤔🤔😎😎😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😘😘😘😘😘😘🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰😍😍😍😍😍😍😍😍😎😎😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😘😘🥰🥰🥰🥰😘😘🥰🥰😘😘😍😍😘😘🥰🥰😘😘😍😍😘😘🥰🥰🥰🥰😘😘😍😍😘😘😘😘😍😍😘😘😘😘😍😍🥰🥰🥰🥰😍😍🥰🥰🥰🥰🥰🥰

@muhammad_haseeb_pk
2 months ago

sir most underrated channel hope you will achieve more and more in future

bretter than million of paid courses

@muhammad_haseeb_pk
2 months ago

I think you are better and way so much good more than codewithharry and Apnacollage good sir really appreciated