Understanding Modules And Its Type In NodeJS

Search for a command to run...

No comments yet. Be the first to comment.
In this tutorial series, we will understand the Fundamentals of NodeJs and learn to use Javascript on the server side.
What is Node.js? Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project! Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. Node.js allo...
You've done it. I've done it. We've all done it.

Your AI Agent Has Amnesia. ByteRover Fixes It — Without Vectors Let's see how to built a customer support agent that remembers everything — using plain markdown files, BM25 search, zero embeddings, a

My GenAI cohort kicked off today, and what a first day! Right away we were reminded that AI isn’t magic — at its core, models like Generative Pretrained Transformers (GPT) are doing one simple thing:

Hello Folks👋, I Recently got Graduated🎓 in 2020, yep in the Pandemic. We all hit very hard by this Corona Pandemic and we all got stuck at home for very long. We all realize the Importance of Socializing, Fitness (Physically as well Mentally), Diet...

So after learning the basic commands of git in our previous section, let's see Git in action. If you already have a project to track your files then you can skip this section and if not then continue. Initializing a Repository from Non-existing Dire...

In NodeJS, every JS file is a module.
Consider modules to be the same as JavaScript libraries. A set of functions, variables, you want to include in other JS files (modules) of your application.
Node.js has a set of built-in modules which you can use without any further installation.
To include a module, use the require() function with the name of the module:
var http = require('http');
Now your application has access to the HTTP module, and is able to create a server:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(3000);
You can create your own modules, and easily include them in your applications.
The following example creates a module that returns a date and time object:
exports.myDateTime = function () {
return Date();
};
Use the exports keyword to make properties and methods available outside the module file.
Save the code above in a file called "myfirstmodule.js"
Now you can include and use the module in any of your Node.js files.
var http = require('http');
var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(3000);
Notice that we use ./ to locate the module, that means that the module is located in the same folder as the Node.js file, if you want to go one folder up use ../
Save the code above in a file called "demo_module.js", and run the file:
node demo_module.js
If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:3000
So In this section, We saw types of Modules in Javascript and how to use them in different situations.