Top Node.JS Built-in modules you need to know

f:id:logisticinfotech:20200627225420j:plain

Either you want it or not, even if you are aware of it or not, if you are working on Node.js, you are frequently using Module service. Modules in Node JS are simple javascript libraries for writing variables and functions. After understanding the modules, you can leverage the power of the modules to do the task that you wanted to do in it. But you don't have to think more about Modules if you are working on a single file or rewriting any javascript commands or just doing some small stuff. But if we find programs with high complexity, we must have to get decent knowledge about Modules first. The best thing about this javascript is, it has a damn extensive collection of modules.

 

Modules

The module is a kind of package consisting encapsulated code, encapsulated code means by a code holding its private variable but cannot be able to access it. Also, if you are practising this module at any other path or program, it won't be able to impact on that code directly, for accessing it, you will have to use Namespaces. In simple words, it's a file that you are creating to use in any different file.

 

There are two types of modules that are mentioned below -

 

Custom Modules - Custom modules are the simple library of javascript which node js developer makes on his own for accessing it into any other different Module. Developing Custom Modules is damn easy.

 

Built-in Modules - Since the Node has created a lot of modules like the file system, path, HTTP, HTTPS, assert, buffer, and much more. You can use these all built-in modules without any of further installation after installing the Node.js package. There are a lot of built-in modules from which major significant modules are mentioned below -

  • assert - This built-in module provides you with a set of assertion tests. This module can be used internally by Node.js, and It is entirely based on the result since if the expression assesses to false(0), the program you are going through will automatically terminate. There are also a lot of modes of assertion such as strict assertion mode, Legacy assertion mode, For using this module, you have to use code [ const assert = require('assert'); ] and for the strict assertion, [ const assert = require('assert').strict; ].
  • path - Path module works the same as strings since it provides the way towards file path. Using the path module is better than that of using strings. It gives us a collection of useful functions for working with paths. For using this module, you have to use code [ const path = require('path'); ].
  • Http - This module is most useful since it acts ditto like an HTTP server. With this module, we can create web servers that will listen for HTTP requests and also we can easily create back-end service for websites, android, and client applications. There are various classes in HTTP module like http.server, http.METHODS, http.agent and much more. Loading the http module is still same as all other modules, use code [ const http = require('http'); ] for executing this module.
  • OS - There are a lot of methods which are available in the OS module, such as os.hostname() for getting the hostname, os.uptime() for acquiring knowledge about the uptime of the machine. Basically, the OS module provides enormous useful methods such as free memory, platform info and much more related to the operating system. For loading the OS module in the program, we have to execute [ const os = require('os'); ] same like all the above modules.

There are still a lot of more built-in modules in Node.js about which we will discuss in the later session.

 

Advantages of Modules

  1. By the module concept, it's possible to allow other files for using the moduled file code by exporting it easily.
  2. Its syntax is available in about all the versions of Node.
  3. A module can export a single default value and multiple values both.
  4. The user can import only the part of the code that they actually need, which makes the program or bundle size small.

Conclusion

Modules are the most useful stuff to show your executions readable and maintainable, and even you can use them damn easily just by executing some simple codes that we've mentioned above. Moreover, you don't have to fix every module in your mind as every module has almost the same execution format. You must have to execute modules in your codes and give it a try at least once for going in deep.