Node.js: Modules - Day Two

Datetime:2016-08-23 05:19:21         Topic: Node.js          Share        Original >>
Here to See The Original Article!!!

NodeJS is used for building asynchronous, event driven and I/O based Applications. Module is one of the major parts of NodeJS. In this article, we will learn about the modules. Module is a set of a reusable code, which encapsulates related code into a single unit of code. Using module, we can insert the related code in a file and use this file, wherever we require. Thus, the code of module doesn’t conflict with the remaining part of the Application.

  1. Node.js: V8 JavaScript Engine - Day One
Node.js supports two type of modules, which are:
  1. Node.js built in modules
  2. User Defined Modules.

Nodejs built in Modules

Nodejs also contains built in modules. You can install these modules, using the NPM (Nodejs Package Manager) and using “require”, you can implement the module in your Application. Nodejs supports a list of the built in modules.

After clicking on any module, you can read out the whole documentation. You will find how you can implement the module in the Application, using “require” method. It helps to find all the properties and the method attached with this module.

Install Module Using NPM

Using NPM. You can install the modules at the local or global level.

Install Module at Local Level

Using “npm install <module_name” command, you can install the module at the local level.

In the image, shown above, we install the “stream” module in our Application. Whenever first module is installed at the local level, it creates a “node_modules” folder and all the installed modules will be present here.

Install Module at Global Level

Using “npm install <module_name> -g” command, you can install the module at the global level. We can’t use the global module in our Application, using the “require” command. These modules can only used by “Command Line Interface(CLI)” function.

Get List of Locally and Globally installed Module

You can use “npm ls” command to get the list of all locally installed modules and “npm ls –g” for the globally installed modules.

Uninstall the Module

Using “npm uninstall <Module_Name>” command, you can uninstall any module.

Update Module

Now, we will learn, how to update the pre installed module . Use “npm update <Module_Name>” command to update any module . This command updates the package.json file of the module.

Use Module Into Code

After successful installation of the module , using “require” method, we can implement the module in our file.


User Defined Module

Till now, we read about the Node.js built in modules. Now, we will learn, how to create a user defined module and its use in our files. First of all , we create a module and name as Main.js.

Main.js file



In this example, we create a Main.js module and Export the CallFun Method of the module. It is very important, that after creating the properties, method and object in the modules, we should export (either using export or module.export) the required property, function or the object otherwise, we can’t use these objects into our JS file, where we require. In JS file, we must use the require method to implement the module into an Application. Require() is a Node.JS function, i.e used to include the Node.JS modules, which are placed in the separate files.

Let us take another example:




In this example, we create a Module.js file and define the two functions in this file. At the end of the file, we write the “module.exports.Method1 = CallFun;”. In this line of code, we use export method. Export is the object, which is actually returned as the result of a require call. In an App.js file, we include the Module.js file, using the require function and take the reference in Call obj. Using Call obj, we called the Method1.


Put your ads here, just $200 per month.