What is a Module?
A module can simply be defined as a section of a program that contains a routine or group of routines.
There are three types of modules in Node:
- Built-in modules
These are modules that come with the Node application by default.
- Custom modules
These refer to modules that are created by the developer to break down application logic into reusable chunks.
- External modules
These are modules that are created by other developers and shared for the community to use.
Creating a custom module
Creating a custom module is as simple as declaring a function that logs a statement to the console.
// greetUser.js
const greetUser = (name = 'stranger') => {
console.log(`Hello ${name}`);
}
Exporting modules
Exporting a module as the default
A module can be used as the default export for a file. To achieve this, we set the name of the module to the exports
property of the module
object.
// greetUser.js
module.exports = greetUser;
This is similar in functionality to JavaScript's export default
.
Exporting a module as a named export
Another way of exporting a module is by assigning an object containing the module names to module.exports
.
// greetUser.js
module.exports = { greetUser };
Same as:
// greetUser.js
exports.greetUser = (name = 'stranger') => {
console.log(`Hello ${name}`);
}
Importing modules
require
Importing modules in Node involves the use of a special function - require
. require
takes in a single argument which represents the path to the file that contains the module to be imported.
require('module');
Built-in modules
Built-in modules are modules that come pre-installed with the Node application by default. Some built-in modules in Node include:
fs
- file systemos
- operating systempath
http
For example, to use the built-in http
module in our application, we would have to import it as follows:
const http = require('http');
Custom modules
Custom modules are those modules that are created by the user.
To make use of our greetUser
module which we created earlier, we will need to import it first.
// index.js
const greetUser = require('./greetUser');
When importing a custom module, we provide a path to the file containing the module which we want to use. When using a relative path, if the source file for the module is in the same directory as the target file, the path is prepended with ./
followed by the name of the file.
External modules
npm
Before external modules can be used in a project, they have to be installed in the root directory of a project.
To achieve this, we use a tool known as node package manager (npm
). npm
is installed with node by default.
To install an external package, we run the following command in the root directory of the project:
npm install <packageName>
// OR
npm i <packageName>
Dev dependencies
A dev dependency is a package that is used for testing and development purposes, but is not used in production. An example of this is the nodemon
package.
To install a package as a dev dependency:
npm install <packageName> -D
// OR
npm install <packageName> --save-dev
Using external modules
Here, we are going to install express, a backend web application framework for Node.js . We run the following command in the terminal:
npm install express
We import external modules the same way we do for built-in modules.
const express = require('express');
Conclusion
I hope I have been able to guide you on using modules in Node. Thanks for reading.
process.exit()
๐