The concept of modules comes from the modular programming paradigm. This paradigm advocates that software should be composed of separate, interchangeable components called modules and that modules should be made by breaking down program functions. A module encapsulates code and data to implement a particular functionality.
What are modules?
The introduction of modularity allowed programmers to reuse pre-written code with new applications. Consider modules, a set of functions you want to include in your application. Modules make a programmer's job easy by allowing them to focus on only one area of the functionality of the software application. Modules are typically incorporated into the software through interfaces.
Node.js includes three types of modules:
- Core modules
- Local modules
- Third-party modules
Let's start by discussing core modules and how to use them.
Core modules
Node.js has a set of core modules that you can use without any further installation. To include a module in a Node.js application, use the require() function with the module's name inside parentheses. These modules load automatically when the Node.js process starts. Core modules can be identified using the node: prefix, in which case it bypasses the require cache. For instance, require('node:http') will always return the built-in HTTP module, even if require.cache has an entry by that name.
One of the core modules is the FS module which includes classes, methods, and events to work with file I/O(Input-Output).
Take a look at the following snippet:
const fs = require("node:fs");
This way your application can access the FS module and you can use its functionality.
Some of several of the core modules in Node.js are:
- HTTP – to make Node.js act as an HTTP server
- OS – provides information about the operating system
- URL – to parse URL strings
- DNS – to do DNS lookups and name resolution functions
Local modules
You can also create your modules and easily include them in your applications as local modules.
Let's create a module that writes a greeting message to the console:
exports.sayHello = function (name) {
console.log("Hello", name);
};
Use the exports keyword to make properties and methods available outside the module file.
Save the code above in a file called "greetingModule.js".
Now you can include and use the module in any of your Node.js files.
const greeting = require("./greetingModule");
Take note that we use ./ to locate the module. This means the module is located in the same folder as the Node.js file.
Third-party modules
You can install third-party modules with the help of Node Package Manager or NPM for short. NPM is a command line tool that can install, update, and uninstall Node.js packages for your application. It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository.
NPM is included by default while installing Node.js. To verify NPM installation, write the following command in the terminal:
npm -v
Let's see how you can install modules using NPM by installing the express module.
npm install express
Now you can import and use the express module in your project by using the require() method.
Conclusion
Modules help us to write clean and reusable codes. There are over 50,000 NPM modules that you can use in order to achieve your goals. All modules that you install with the npm command, are stored in the node_modules folder. This folder should always be in the root path with the package.json file. You can check the versions of installed packages in the package.json file. Although core modules for Node.js and NPM modules will be enough for most of our use cases, you might need to use some custom modules as well.