Basics Of Node.js Modules

In this article, we will be learning the basics of Node.js Modules. There are several questions when talking about Node.js Modules:

  • Why should we know about the Node.js Modules?
  • What are Node.js Modules?
  • Where are Node.js Modules used?

Let’s discuss this from the beginning.

Nowadays, most of the web developers are working with new technologies like Angular, React, and Vuejs as frontend and Express.Js, Meteor web framework, or SharePoint Framework in JavaScript. But every developer faces issues to understand the pattern of writing the code, or we can say, the ecosystem used in the packages\libraries.

If each developer is aware of the NodeJS Modules, it will be helpful in understanding the ecosystem they are working in.

As we know, Node.js is a platform that provides an environment to execute JavaScript outside of the web browsers; when we execute JavaScript in Node.js, it will change the scope of the JavaScript.

In web browsers, the top-level scope is a global scope. If we declare a variable outside of the function, it becomes GLOBAL and the scope of the variable is said to be Global.

Please refer to this link to learn about JavaScript Scope in web browsers. 

But this is different in NodeJS; the top-level scope is not the global scope, if we declare a variable inside NodeJS Module, it will be local to that module.

What is NodeJs Module?

NodeJS Module is a set of functions and variables to implement common functionality organized in single or multiple JavaScript files to reuse it throughout the application. We can say it's similar to JavaScript Libraries.

Node.js implements CommonJs Module Standard

We need to use “require” keyword to include a module, and “exports” to make a module available.

Let’s take an example,

  • We have installed Nodej.s on a Windows system. so, open the command prompt and make a folder named “demo”.

    • mkdir demo
    • cd demo
    • npm init

  • It will create package.json in demo folder.
  • Now, we are creating our own module named “demoModule.js” and writing the following code.
    1. exports.getCurrentDateTime = function() {  
    2.     return Date();  
    3. };  
  •  After that, include this module to “example.js” and write following code.
    1. var dt = require('./demoModule').getCurrentDateTime;  
    2. console.log(dt());  
  • Enter “node example.js” in command prompt. 

 

NodeJS System Modules

Node.js has a built-in set of modules called System Modules.

  • events - To handle events
  • fs - To handle the file system
  • http - To make Node.js act as an HTTP server
  • https - To make Node.js act as an HTTPS server.
  • net - To create servers and clients
  • os - Provides information about the operation system
  • path - To handle file paths
Let’s take an example of HTPP module.
  • Here we have used http to create HTTP server that will listen to client request to server port and provide a response back to the client
  • In the above demo example, we include http module and create http server that will write “hello world” to the web browser.
    1. var http = require('http');  
    2. //create a server object  
    3. http.createServer(function(req, res) {  
    4.     // write response to client  
    5.     res.writeHead(200, {  
    6.         'Content-Type''text/plain'  
    7.     });  
    8.     res.write('Hello World!');  
    9.     res.end();  
    10. }).listen(8000);  
    11. console.log("Application running on port 8000");  
  • Enter “node example.js” in command prompt.

There are several patterns of write modules in NodeJs. Please refer to this link to learn more about NodeJs module pattern. 

Conclusion

In this article, we have learned about NodeJS Modules and how modules play a role while we are working with JavaScript.

Thank you and please share your thoughts & feedback.


Similar Articles