Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Explaination of Module in Node Js with Example ?

What is Module in Node Js?

In Node.js, a module is essentially a reusable piece of code that encapsulates related functionality. Modules help in organizing code into separate files or packages, making it easier to manage, reuse, and maintain. There are different types of modules in Node.js:

  1. Creating a Module: To create a module, you simply define your functionality in a separate file. This file can contain variables, functions, classes, or any other JavaScript code. By default, everything defined in a file in Node.js is scoped to that file, meaning it’s not automatically accessible from other files unless explicitly exported.
  2. Exporting from a Module: To make functionality available outside the module, you use the module.exports or exports object to export the desired variables, functions, or objects. This makes them accessible to other modules that require or import them.
  3. Importing/Requiring a Module: To use functionality from a module in another file, you use the require() function to import the module. This function returns the exported functionality from the module, allowing you to use it within your code.

Example:-

1st create math.js file and put below code

function add(a,b){
    return a + b;
}

function sub(a,b){
    return a-b;
}
module.exports = {
   add,sub
}

Next to create hellow.js file and put below code

const math = require("./math");

console.log("math value is ",math);

Next to run below command

npm run start
Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

Youtube Subscriber Count in ReactJs

In this tutorial i’m going to learn how to count YouTube Subsribers count, views count as well, as define below. In order to install your app, first…

Hellow word in Node js with example ?

What is Node js ? Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is…

What is Node JS Explaination ?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. It is built on Chrome’s V8 JavaScript engine and allows…

Building HTTP Server in NodeJS with example ?

What is Node js ? Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is…

What is Node JS with example ?

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is built on the V8 JavaScript…

How to Building HTTP Server in NodeJS

What is Node js ? 1st step create below file and put below code Next go to package.json and put below code Next to run below command…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x