What are the types of Node.js modules?
Node.js is an environment used in web development to run JavaScript code outside of web browsers. It allows us to create fast and efficient server-side applications. To use Node.js, we must ensure we have Node installed on our system. Our Answer on How to install Node.js on Ubuntu gives us a guideline for the installation process.
Types of Node.js modules
Node.js provides us with various modules to make our tasks easier and faster to perform. There are three types of Node.js modules. In this Answer, we will discuss these types in detail.
Core modules
The core modules in Node.js are the built-in modules that are installed with the installation of Node to our system. We just use the required statement to access them.
let coreModule = require(coreModuleName)
coreModule: The variable in which we store the core module.coreModuleName: The core module we want to import.
Note: When we use
require()to import a module, the returned value can vary depending on the module's type.
Examples
Following are some examples of the core modules:
Examples of core modules
Core Module | Function |
fs | provides functionalities to manipulate files |
http | helps to create http requests |
path | helps to deal with paths and directories of files |
os | provides information about the operating system |
url | provides functionalities to manipulate urls |
Code sample
Let's understand the core module further with a sample code. Here we use the URL module.
const url = require('url');// Our URLconst myURL = 'https://www.example.com:8080/path/to/resource?id=123&name=John%20Doe';// We parse the Urlconst parsedURL = new URL(myURL);// We print the url componentsconsole.log('Protocol:', parsedURL.protocol);console.log('Host:', parsedURL.host);console.log('Pathname:', parsedURL.pathname);console.log('Query Parameters:', parsedURL.searchParams.toString());
Let's understand our code.
Line 1: We require the core module.
Line 7: We define a URL string that we will parse using the functionalities in the URL module.
Lines 10–13: We print the URL's components on the console.
Local modules
The local modules in Node.js are the modules that we create ourselves and make available to be used in our code. We define the functionality for these modules in a JavaScript file. To use these modules, we require them in our code.
let localModule = require(./localModuleName)
localModule: The variable in which we store the local module../localModuleName: The relative path to the JavaScript file that contains the local module.
Code sample
Let's understand the local module further with a sample code.
// Requiring the local moduleconst calculate = require('./calculate');// Using the functions from the local moduleconst num = 5;const squaredNumber = calculate.square(num);console.log(`Square of ${num} is: ${squaredNumber}`);const cubedNumber = calculate.cube(num);console.log(`Cube of ${num} is: ${cubedNumber}`);
Let's understand what's happening in the code.
In calculate.js:
Lines 2–4: We define a function for calculating the square of the
number.Lines 7–9: We define a function for calculating the cube of the
number.Lines 12–15: We use the
module.exportsstatement to make these functions accessible outside this module.
In index.js:
Line 2: We require the local module.
Line 5: We define a variable we calculate the square and the cube of.
Lines 6–7: We call the function
squareof the local module. Then we print the output we get on the console.Lines 9–10: We call the function
cubeof the local module. Then we print the output we get on the console.
Third-party modules
The third-party modules in Node.js are those we install globally or on our local machines from external sources, typically via the Node package manager. To install them using npm, we can use the following command:
npm install thirdPartyModuleName
thirdPartyModuleName: The third-party module that we want to install on our local machine.
Note: We can also use
npxoryarnto install the third-part module.
After installing the third-party module, we can require it in our code using the following command:
let thirdPartyModule = require(thirdPartyModuleName)
thirdPartyModule: The variable in which we store the external third-party module.thirdPartyModuleName: The external third-party module we want to import.
Code sample
Let's understand the third-party module further with a sample code. We will use the lodash module, which is a third-party module useful in the manipulation of arrays. To use this external module, we first install it using the following command:
npm install lodash
Now, we can use this module in our code. This is demonstrated in the following code:
// Requiring the 'lodash' third-party module
const calculate = require('lodash');
// Sample data
const numbers = [1, 2, 3, 4, 5];
// Using 'lodash' functions
const sum = calculate.sum(numbers);
console.log('Sum:', sum); // Output: Sum: 15
Let's understand what is happening in our code.
Line 2: We require the third-party module
lodash.Line 5: We define the array data.
Lines 8–9: We use the
sumfunction from the module to calculate the sum of the array. We then display the output on the console.
Conclusion
In this Answer, we explored the different types of Node.js modules that we can use to enhance and improve our web applications. These modules play a crucial role in organizing our code and promoting re-usability.
Free Resources