Open In App
Related Articles

Node.js Local Module

Improve Article
Save Article
Like Article

Node.js comes with different predefined modules (e.g. http, fs, path, etc.) that we use and scale our project. We can define modules locally as Local Module. It consists of different functions declared inside a JavaScript object and we reuse them according to the requirement. We can also package it and distribute it using NPM. 

Defining local module: Local module must be written in a separate JavaScript file. In the separate file, we can declare a JavaScript object with different properties and methods. 

Step 1: Create a local module with the filename Welcome.js 


const welcome = {
    sayHello: function () {
        console.log("Hello GeekforGeeks user");
    currTime: new Date().toLocaleDateString(),
    companyName: "GeekforGeeks"
module.exports = welcome

Explanation: Here, we declared an object ‘welcome’ with a function sayHello and two variables currTime and companyName. We use the module.export to make the object available globally. 

Part 2: In this part, use the above module in the app.js file. 


const local = require("./Welcome.js");

Explanation: Here, we import our local module ‘sayHello’ in a variable ‘local’ and consume the function and variables of the created modules.


Hello GeekforGeeks user
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 11 Apr, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials