Node.js: Modules - Day Two

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.

Nodejs 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.

Nodejs built in Modules

Nodejs built in Modules

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.

command

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.

module
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.

Command

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.

Command

Uninstall the Module

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

Command

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.

Command

Use Module Into Code

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

Example

Code

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

code

App.js

code

Output

Output

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:

Module.js

Module.js

App.js

App.js

Output

Output

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.

Up Next
    Ebook Download
    View all
    Learn
    View all