Node.js – Modules


Node.js – Modules



”;


A module in Node.js is a collection of independent and reusable code that can be imported into any Node.js application. As the name suggests, modules enable a modular and structured approach for developing a Node.js application. Instead of putting all the functions, classes and methods of an application in a single .js file, these resources are arranged in separate files (called modules) based on their relevance. This gives a better control over the maintenance and troubleshooting of the Node.js application.

Node.js runs on V8 JavaScript engine, which interprets the JavaScript code. All the server-side processes are handled by the relevant Node.js module imported into the application by the require() function. A Node.js module is a library of functions, classes and other reusable code, stored in one or more .js files.

A typical example of importing a Node.js module is how a Node.js server is launched. The createServer() function defined in the http module is needed to start the Node.js server. Hence, before calling the http module needs to be imported.


http = require(''node:http'');
listener = function (request, response) {
   ...
   ...
};

server = http.createServer(listener);
server.listen(3000);

Types of Modules

Each module in Node.js has its own context, and implements CommonJS modules standard. The modules in Node.js can be of any of the following three types −

Third-party modules

These modules are developed by independent developers and made available for use on the NPM repository. You should install a module either globally or locally in a Node.js project folder, if you want to incorporate its functionality in your application.

The Express module is an example of such a module. To install, use any of the following commands −


npm install express -g //install globally
or
npm install express –save //install locally

You can then import the installed module in your application. For example −


var express = require(''express'');

Built-in modules

The Node.js runtime software comes with the V8 JavaScript engine, bundled with a number of core modules, that perform important server-side tasks, such as managing event loop, perform file IO and operating system-specific functions etc. The examples of built-in or core modules are http, fs, console etc. These modules are pre-installed, but you must import them with require() function (except for a few modules such as process, buffer and console modules, they are global objects). For example −


var fs = require(''fs'');

Local modules

A local module is a .js file, which stores definition of one or functions or classes required for your Node.js application. Such a module is present locally in the same Node.js application folder, which also should be included in the application with require() function.

Every .js file in a Node.js application has a special module object. It’s exports property exposes a certain function, object or a variable from the .js file to the code outside.

Example

The following functions.js file includes three functions that return value of x raised to y, square root of x, and log value of x. These functions are exported for external consumption.


exports.power = function powerval( x, y) {
   var val = Math.pow(x,y);
   return val;
}

exports.root = function rootval(x,y) {
   var val = Math.sqrt(x);
   return val;
}

exports.log = function logval(x) {
   var val = Math.log10(x);
   return val;
}

We shall now use these exported functions in another Node.js application main.js. First import the local functions module and then call the functions imported from it.


const mymodule = require(''./mathfunctions'');

console.log("10 Raised to 3:" + mymodule.power(10,3));
console.log("Square root of 10: "+ mymodule.root(10));
console.log("Log of 1000: " + mymodule.log(1000));

Output


10 Raised to 3:1000
Square root of 10: 3.1622776601683795
Log of 1000: 3

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *