DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 3 of NodeJS||Important Modules

Hey reader👋Hope you are doing well😊
In the last post we have discussed about how to get started with NodeJS. In this post we are going to see Modules in NodeJS.
So let's get started🔥

What are Modules?

Consider modules as JavaScript libraries that provides different functionalities for development. Modules can be a single file or a collection of multiple files/folders. The reason programmers are heavily reliant on modules is because of their reusability as well as the ability to break down a complex piece of code into manageable chunks.
Node.js treats each file in a Node project as a module that can export values and functions from the file.
Image description
Here utility.js is a module but it is not exporting anything right now.

exports in NodeJS -:
We can export functions and properties either by using module.exports or exports. The exports keyword is a reference to the exports object in the modules object.

Different types of NodeJS Modules

There are three types of modules-:

  • Core Modules

  • Local Modules

  • Third Party Modules

Core Modules

Node.js has many built-in modules that are part of the platform and come with Node.js installation. These modules can be loaded into the program by using the required function.
const module=require(module_name)
The require() function will return a JavaScript type depending on what the particular module returns.
Some of the Core modules are -:

  • http -: creates an HTTP server in Node.js.

  • https -: creates an HTTPs server in Node.js.

  • fs -: used to handle file system.

  • path -: includes methods to deal with file paths.

  • os -: provides information about the operating system.

  • process -: provides information and control about the current Node.js process.

  • querystring -: utility used for parsing and formatting URL query strings.

  • url -: module provides utilities for URL resolution and parsing.

  • assert -: set of assertion functions useful for testing.

  • tls -: to implement TLS and SSL protocols.

  • net -: to create servers and clients.

  • crypto -: to handle OpenSSL cryptographic functions.
    These are some of the important built-in modules.

Local Modules

We can create our own module too apart from built-in and third party modules.
Let's create a simple calculation module-:

  • Step1 -> Create Calculation.js file-:
    Image description
    Let me show you module object here-:
    Image description
    You can see that we are exporting four functionalities here.

  • Step 2 -> Use the module in script.js file -:
    Image description
    Here we have imported the module using require keyword.

  • Output ->
    Image description
    Easy huh🙃

Third-party modules

Third-party modules are modules that are available online using the Node Package Manager(NPM). These modules can be installed in the project folder or globally. Some of the popular third-party modules are Mongoose, express, angular, and React.
Example -:

  • npm install express
  • npm install mongoose
  • npm install -g @angular/cli

So this was all about modules. I hope you have understood this blog. In the next blog we will see one another way of using module in a file. Till then stay connected and don't forget to follow me.
Thankyou🤍

Top comments (4)

Collapse
 
drignet profile image
Odibe Ekene

We are following

Collapse
 
akshat0610 profile image
Akshat Sharma

I am so glad that you are liking my content. Thanks:)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.