What is the NodeJS require Module?

Modules in Node.js are very important. In Node.js each file is considered as a module. It makes use of the CommonJS module system namely NodeJS require, exports, and import.

Node modules help us developers reuse our code. With help of the Node Package Manager (npm), we can create our own modules and make them available for the community. NPM also lets us use the modules of other developers.

Wish to offer something useful to the Community? Here’s how you can Build and Publish an NPM Package!

NodeJS Require Module

NodeJS require is the simplest way to include modules that reside in separate files. It also allows you to consume modules. We can make use of the core built-in modules by Node.js, community-based modules that exist in the node_modules directory of your project, and local modules created by you.

Let us elucidate with some examples for including and consuming modules.

Including a separate file:

Let us consider a file named city.js.

const country = require('./country.js');
console.log(`The city of Muscat is in the country ${country.name}`);

On the first line, the city.js file is loading the module country.js which exists in the same directory as city.js.

Consuming Built-in Core Modules

Let’s say we want to create a boilerplate for our Node.js projects. We will need to use the ‘fs’ i.e. file system module by Node for the purpose.

const fs = require('fs');
const folderName = process.argv[2] || 'PROJECT';

try {
    fs.mkdirSync(folderName);
    fs.writeFileSync(`${folderName}/index.html`, '');
    fs.writeFileSync(`${folderName}/app.js`, '');
    fs.writeFileSync(`${folderName}/style.css`, '');
} catch (error) {
    console.log(error);
}

Here, we are simply creating a directory by any name the user wants or, it will be named as ‘PROJECT’ if he/she hasn’t named it.

Then inside the project directory, we are creating 3 files namely, index.html, app.js, and style.css.

Including third-part Node Modules

Third-party node modules are nothing but the modules created by other developers and published on NPM. These modules once installed, reside in the node_modules directory.

Let us say we want to include the Express module in our Node app.

Installing a module from NPM:

npm i express

Including Express from the node_modules directory:

const express = require('express');

So these are basically the main areas where you will be making use of the require module in your day-to-day coding life.

An Extra Note

The rule can be quite complex as to where require looks for files. However, a simple rule of thumb is, if the file doesn’t start with a ‘./’ or a ‘/’, it is either considered a core built-in module or a dependency (3rd party module). But before, the local directory is checked.

If the file that called require, begins with a ‘./’ it is considered a relative file to the file. Or, if the file begins with a ‘/’, it is considered an absolute path.

You may even choose to omit the file extension name such as ‘.js’ as the required module automatically appends when needed.

If in case the file name passed to require turns out to be a whole directory, it will then look for a package.json in that directory. It will then load the file referenced in the main property. Or, it will simply look for an index.js.

Conclusion

NodeJS require module helps to include files that exist separately. It is a core built-in module and considers every file as a separate module. You can use your own modules as well as the core modules built by Node.js and other developers just with the required module’s help!

Read More: Node.js Tutorial for Beginners Step by Step With Examples

Noteworthy References

Node.js Require API – Official Docs

Aneesha S
Aneesha S
Articles: 172