DEV Community

kamran
kamran

Posted on

Modules in Node JS

Code With Modules
If you are moving to a new house, packing and moving your stuff is always a headache. To avoid the complexity and burden of moving, you can always keep your home stuff sorted and organized and when you want to move, you can simply put similar kinds of stuff in the same moving boxes. You can name the boxes according to what it contains and that will help you pack and unpack effortlessly. This technique will help you be organized and all your stuff will be easily reachable as you know where exactly to find them.

Same goes for software applications, in order to make your application easy to access and organize, you have to pack them properly in boxes and name them conveniently. In Node JS, these boxes are known as "Modules". So now you know why exactly we use modules, let's dive more into this.

Node JS Project with Modules
Basically modules are objects which may contain one or more properties and the best part of a module is you can export and import modules from one file to another and in that manner you can beautifully arrange all the content of your application in a systematic and organized manner.

Node JS come with pre installed modules which you can use for doing the basic tasks in your application like http module which can be use to handle the http server and it also have a filesystem module which you can use to play around with files and folder in your application.When you install node.js in your system these pre-installed modules are installed by default and when you want to use it you can simply import and use it.

You can also create your own module in a Node JS application. Once you create a module you can export it and then you can import it in another file to use it. You can also share that created module with other users to use that and they can use that module in their application, so, basically module can be treated as a box which you can wrap the functionality within and give it to others as well to be used and all the the functionalities in that box can be used by any other user.

A good example of this will be, you can create a class as a module and export that and then Import that class in any file you want. You can also export multiple objects from a single module and then use that module as an imported object in another file.

Apart from the inbuilt modules and self created modules, you also have external modules available in node package manager which you can install and import in your application and use the external functionality in your application. So you can see how modules made your life easy in building your application you don't have to to build each and every thing in order to build your application you can simply import the module from other packages and then you can use that and build your application on top of that.

I have also created a video on how to work with modules in node JS and I hope this video will help you to understand the basic concepts of modules with actual coding.

Thanks for reading, happy Koding everyone!!

Latest comments (0)