Skip to main content

Modules

Aahil...About 2 minDocs-Basics

In this tutorial, we'll explore how modules work in Node.js, including built-in modules and how to create and include your own modules.

What is a Module in Node.js?

In Node.js, modules are similar to JavaScript libraries. They consist of a set of functions or pieces of code that you can include in your application.

Built-in Modules

Node.js comes with a rich set of built-in modules that you can use right out of the box, without any additional installation. These modules provide essential functionalities for various tasks.

Here's a list of some built-in modules available in Node.js version 6.10.3:

ModuleDescription
assertProvides a set of assertion tests
bufferHandles binary data
child_processRuns a child process
clusterSplits a single Node process into multiple processes
cryptoHandles OpenSSL cryptographic functions
dgramProvides implementation of UDP datagram sockets
dnsPerforms DNS lookups and name resolution functions
domainDeprecated. Handles unhandled errors
eventsHandles events
fsHandles the file system
httpMakes Node.js act as an HTTP server
httpsMakes Node.js act as an HTTPS server
netCreates servers and clients
osProvides information about the operating system
pathHandles file paths
punycodeDeprecated. A character encoding scheme
querystringHandles URL query strings
readlineHandles readable streams one line at a time
streamHandles streaming data
string_decoderDecodes buffer objects into strings
timersExecutes a function after a given number of milliseconds
tlsImplements TLS and SSL protocols
ttyProvides classes used by a text terminal
urlParses URL strings
utilAccesses utility functions
v8Accesses information about V8 (the JavaScript engine)
vmCompiles JavaScript code in a virtual machine
zlibCompresses or decompresses files

To include a built-in module, use the require() function with the name of the module:

var http = require('http');

Now your application has access to the HTTP module, allowing you to create a server like this:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');
}).listen(8080);

Create Your Own Modules

You can also create your own modules and easily include them in your applications. Let's create an example module that returns the current date and time:

// myfirstmodule.js

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

Use the exports keyword to make properties and methods available outside the module file.

Include Your Own Module

Now that you've created your own module, you can include and use it in any of your Node.js files. Here's how to use the "myfirstmodule" module in a Node.js file:

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(8080);

Notice that we use ./ to locate the module, indicating that the module is located in the same folder as the Node.js file.

Save the code above in a file called "demo_module.js", and initiate the file:

C:\Users\Your Name>node demo_module.js