NodeJS Modules: Core, Local and Third Party

NodeJS modules are something that makes your coding life easy by organizing complex functionality in single or multiple files of JavaScript that can be reused. These are mini libraries that consist of code that performs specific activities and can be used later so that we do not need to code that functionality every time.

Also Read: What is Node.js?

Types of Modules in NodeJS

  1. Core Modules
  2. Local Modules
  3. Third-Party Modules

NodeJS Core Modules

These are the modules pre-built with NodeJS. These are pre-written in the NodeJS system, whenever you will install NodeJs, they will be automatically installed in your system. Some of the most popular core modules are discussed below.

httpUsed for making all types of HTTP requests such as GET, POST, PATCH, DELETE, etc.
fsA File System Module is used for working with files present in the operating system.
pathIt can be used to work with file paths.
processIf you need to code on the operating system’s process level, you can use this module.
const path = require('path')

To import a module, you can simply use require() function and pass in that module’s name. The require() function will return the module and save it in a variable.

NodeJS Local Modules

Sometimes developer needs the same functionality in different places in their software so they create their own local modules. These are nothing but those modules which are created by the developer and are not pre-built in Node JS. They are used to make development life more effortless and fast because they automatically decrease code length. Let’s create our own local module.

// greetingsModule.js
// Simple Greetings Module

exports.sayGoodMorning = function(name){
    console.log(`Good Morning ${name}!`)
}

exports.sayGoodAfternoon = function(name){
    console.log(`Good Afternoon ${name}`)
}

exports.sayGoodNight = function(name){
    console.log(`Good Night ${name}`)
}

In the above code, we have created three functions and exported them using the exports keyword. This keyword will export these functions so that they can be imported to use in other files as modules.

const greetingsModule = require('./greetingsModule')

greetingsModule.sayGoodMorning('Sushant')
greetingsModule.sayGoodAfternoon('Sushant')
greetingsModule.sayGoodNight('Sushant')

Now, we have imported the file created in the above step using the require() method. Then used the dot operator and called all three functions. The output of all three functions can be shown below.

Output

NodeJS Third-Party Modules

Most of the time while coding required several kinds of functionality. Now to do this we have two methods. The first method is we can implement complete functionality on our own and the second method is we can use any third-party module which will provide us with the same functionality. We usually go for the second option. So third-party modules are those modules that are publicly available and developed by someone else. These are found on repositories such as npm.

Some of the most popular third-party modules are:

Installation:

To install any third-party module we need a package manager. The most popular package manager is npm. When you install a NodeJS in your system, npm is also automatically installed. Below is the command to install any package from npm directly into your project.

npm install mongoose

The above command will install a module “mongoose” in your project.

Also, we can install the module globally so that it can be accessed by any of your projects, you can use the same command with an additional attribute “-g” to install a module globally.

npm install -g mongoose

Here -g means global installation.

Summary

Modules are an essential part of a developer’s life. As they are very powerful and make things easy. There are three types of modules in NodeJS, the core module which is a built-in module that comes with Node itself, a local module that can be created manually and a third-party module. These are very helpful if you want to save time. After reading this post, you are aware of modules, their types, use cases, and how you can install third-party Modules. Thank you for reading this post, we hope it was helpful for you.

Reference

https://nodejs.org/api/modules.html

Sushant Dhiman
Sushant Dhiman
Articles: 14