Node.js Modular Programming

      No Comments on Node.js Modular Programming

Node.js uses modular programming where different functionalities are divided into different modules. Based on the need, modules can be included.

Modules can built in Node.js or it could be custom modules as well.

e.g. http is a built-in modules.

Categories of Modules in NodeJs:

There are two categories of modules in Node.js.

I.) Built-in Modules
II.) Custom (User-defined) Modules

Built-in Modules :

These are those modules which comes by defualt with Node.js or you can install using npm on the machine where Node.js is installed.

e.g. http is a built in modules.

To include a built-in modules , you need to use require() function.
e.g. to include http modules, the below code will work:

var http = require(‘http’);

Custom (User-defined) Modules:

These are those modules which are built and defined by users using “exports” keyword. custom modules can be built using “exports” keyword.

Let us take an example of a custom module in Node.js.

1.) Write the function logic.

exports.printHelloToConsole = function(){



2.) Save this function with extension *.js where * represents a meaningful name given by user according to the functionality.

suppose it is saved as printHello.js in the path c:\masterjavatutorial\ (in windows)

Now, this module can included wherever you want using the below command :

var helloWorld = require(‘c:\masterjavatutorial\printHello’);

Leave a Reply