node.js

Node.js: Working With Modules

Modules are generally set of functions that you can include in your application.

These could be built-in functions that are part of the Node.js library or imported from a third-party library.

Built-in Modules

Node.js has a set of built-in modules which you can use without any further installation.

These include:

ModuleDescription
assertProvides a set of assertion tests
bufferTo handle binary data
child_processTo run a child process
clusterTo split a single Node process into multiple processes
cryptoTo handle OpenSSL cryptographic functions
dgramProvides implementation of UDP datagram sockets
dnsTo do DNS lookups and name resolution functions
domainDeprecated. To handle unhandled errors
eventsTo handle events
fsTo handle the file system
httpTo make Node.js act as an HTTP server
httpsTo make Node.js act as an HTTPS server.
netTo create servers and clients
osProvides information about the operation system
pathTo handle file paths
punycodeDeprecated. A character encoding scheme
querystringTo handle URL query strings
readlineTo handle readable streams one line at the time
streamTo handle streaming data
string_decoderTo decode buffer objects into strings
timersTo execute a function after a given number of milliseconds
tlsTo implement TLS and SSL protocols
ttyProvides classes used by a text terminal
urlTo parse URL strings
utilTo access utility functions
v8To access information about V8 (the JavaScript engine)
vmTo compile JavaScript code in a virtual machine
zlibTo compress or decompress files
Node.js built-in modules

Using a Module

To use or require a module, we use the require() function followed by the name of the module

Example:

let http = require('http');

Creating Your Own Module

You can create your own modules and include them in your project.

First, let us create a module called Time.js as follows

exports.mytTime = function() {
    return Date();
}

Next, let us create a main.js file where we can use this newly-created module

let http = require("http");
let date = require('./Time');

http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body as "Hello World"
   response.end(`Hello world. Thank you The time is ${date.mytTime()}`);
}).listen(80);

Next we run the file on port 80 and we get the response printed on the screen.

This is how we can create modules that we can use in our project

Recent Posts

Google Announces that AI-developed Drug will be in Trials by the End of the Year

Isomorphic Labs, a drug discovery start-up launched four years ago and owned by Google’s parent…

15 hours ago

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

3 days ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

6 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

1 week ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago