What are modules in node JS

A module in node.js is a functionality which organized in single and multiple JavaScript files which can be reused in all node.js application. In this tutorial, you will learn what are modules in node JS with examples.

Node.js is a lightweight JavaSrcipt based server-side framework. The core modules of nodejs include the minimum functionalities which load automatically when the Node.js process starts.

Node.js Module Types

There are three types of Node.js modules:

  • Core Modules
  • Local Modules
  • Third Party Modules

Built-in Modules

Below is the list of built-in node.js modules, which can we use in any node.js applications.

Core Module Description
http http module includes classes, methods and events to create Node.js http server.
url url module includes methods for URL resolution and parsing.
querystring querystring module includes methods to deal with query string.
path path module includes methods to deal with file paths.
fs fs module includes classes, methods, and events to work with file I/O.
util util module includes utility functions useful for programmers.

Here is an example of node js http module:

Want to execute the above example. See how to run the node.js application.

How to Create Your Own Nodejs Module

Apart from the built-in nodejs modules, you can also create your own modules. Below is an example of a custom nodejs module which prints the system date and time like Mon Oct 15 2018 12:15:24 GMT+0530 (India Standard Time).

Copy paste below code in your index.js file and execute it using the node index.js command in your command line interface.

nodejs custom modules

Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn more node.js tutorials.

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts: