Vaidikalaya

Nodejs Local Modules


A local module in Node.js is a custom module created by the user within their project. These modules are specific to the application and are not part of the Node.js core modules or third-party packages installed via npm.

When working with local modules in Node.js, there are three main steps you need to follow: Create, Export, and Import. Here's a brief overview of each step:


Create, Export, Import - Syntaxes

Syntax 1: When the module has a single function

module.js
function myFunction() {
    // Function logic here
}
// Export the function
module.exports = myFunction;

Import

Once the module is exported, you can import it into another file using the require function.

app.js
// Import the function
const myFunction = require('./module');

// Use the function
myFunction();

NOTE:

⮚ The require() function is a built-in function in Node.js that is used to import modules. It reads the JavaScript file specified in the path you provide and returns the module.exports from that file.

⮚ The ./ part means that the file is in the same directory as the file where you're writing this code. If the file is in another directory, you would adjust the path accordingly (e.g., ../module for a file in the parent directory).


Syntax 2: When the module has multiple functions, objects, or variables:

javascript
function myFunction1() {
    // Function logic here
}

function myFunction2() {
    // Function logic here
}

function myFunction3() {
    // Function logic here
}
 
// Export the function
module.exports = {myFunction1, myFunction2, myFunction3};

import

When the module has multiple functions then import and use it like this:

javascript
// Import the module
const myModule = require('./module');

// Use the functions
myModule.myFunction1();
myModule.myFunction2();

Examples to illustrate:

Example 1: Single Function

To create a file named greeting.js, you can place it either in a modules directory or the same directory as your main file (app.js). Here's how you can do it:

E:\node-tutorial\modules\greeting.js
function hello(){
    return "Hello Vaidikalaya";
}
module.exports=hello;

In another file, say app.js, you can import and use the hello function from greeting.js:

E:\node-tutorial\app.js
const hello=require('./modules/greeting')
const res=hello();
console.log(res);

/*Output
    Hello Vaidikalaya
*/

Example 2: Multiple Function

Let's say you want to create a local module that performs basic arithmetic operations like addition and subtraction. You can create a file named mathOperations.js:

E:\node-tutorial\modules\mathOperation.js
function add(a, b) {
    return a + b;
}

function subtract(a, b) {
    return a - b;
}

module.exports = {add,subtract};

To use this module in another file, you would require it using the require function:

E:\node-tutorial\app.js
const math = require('./modules/mathOperations');

const sum = math.add(5, 10);
const difference = math.subtract(10, 5);

console.log(`Sum: ${sum}`);                
console.log(`Difference: ${difference}`);

/*Output
    Sum: 15
    Difference: 5
*/

Local modules in Node.js are a powerful way to create, organize, and reuse code specific to a project. They help maintain a modular architecture, making the application easier to manage, test, and scale. By encapsulating functionality in local modules, developers can improve code clarity, reduce duplication, and promote best practices in software development.