A local module is a self-contained block of code that can be reused throughout an application. It is a way of organizing your code into small, manageable chunks that can be imported and exported as needed. Local modules allow you to keep your code organized and easy to maintain.
Node.js provides a simple way of creating local modules using the "exports" keyword. When you export a function or an object from a module, you can then import it into other files using the "require()" function.
Let's create a simple local module in Node.js that exports a function called "add". The function takes two parameters and returns their sum. Here is the code for the "add.js" file:
const add = (a, b) => { return a + b; }; exports.add = add;
In the above code, we define a function called "add" that takes two parameters and returns their sum. We then export this function using the "exports" keyword.
To use this local module in another file, we can use the "require()" function to import it. Here is the code for the "index.js" file:
const addModule = require("./add"); console.log("Local Module"); const total = addModule.add(10, 5); console.log("Total : " + total);
In the above code, we use the "require()" function to import the "add.js" module. We then log "Local Module" to the console and call the "add()" function with the values of 10 and 5. We then log the total to the console.
Using local modules in Node.js offers several benefits:
In addition to exporting a single function or object, you can also export multiple functions or objects from a local module in Node.js. Here is an example of how to create a local module that exports two functions, "add" and "sub", that perform addition and subtraction respectively.
// math.js const add = (a, b) => { return a + b; }; const sub = (a, b) => { return a - b; }; module.exports = { add: add, sub: sub, };
In the above code, we define two functions, "add" and "sub", that perform addition and subtraction respectively. We then export these functions using an object that maps the function names to their respective functions.
To use this local module in another file, we can use the "require()" function to import it. Here is the code for the "index.js" file:
// index.js const math = require("./math"); console.log("Local Module"); const sum = math.add(10, 5); console.log("Sum: " + sum); const difference = math.sub(10, 5); console.log("Difference: " + difference);
In the above code, we use the "require()" function to import the "math.js" module. We then call the "add()" function with the values of 10 and 5, and log the result to the console. We then call the "sub()" function with the same values, and log the result to the console.
Using multiple functions in local modules can offer several benefits:
/*------------------------------------------------ Program : 1 write a Node.js module named circle.js that exports a function calculateArea which takes the radius of a circle ascircle.js
input and returns the calculated area? Remember to use the value of PI as 3.14. ------------------------------------------------*/ const circle = require("./circle"); console.log("Area of a Circle is : ", circle.area(5));
const PI = 3.14; function calculateArea(radius) { return PI * radius * radius; } module.exports = { area: calculateArea, };
In conclusion, local modules are an essential part of Node.js development. They allow you to break your code into small, manageable chunks, and keep your code organized and easy to maintain. By exporting functions or objects from a module, you can reuse your code throughout your application, making it easier to scale as it grows. We have also gone through an example of how to create a local module that exports multiple functions, and how to use them in another file. Using local modules in your Node.js applications can help you keep your code organized, easy to maintain, and scalable. We hope this blog post has been helpful in understanding how to use local modules in your Node.js applications.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions