Modules

Node.js uses a modular system. That is, all the built-in functionality is divided into separate packages or modules. A module represents a block of code that can be reused in other modules.

If necessary, we can connect the modules we need. What built-in modules are in node.js and what functionality they provide can be found in the documentation.

To load modules, the function is used require(), to which the name of the module is passed. For example, in the first application from the previous topic, the http module was needed to receive and process the request:

const http = require("http");

After receiving the module, we will be able to use all the functionality defined in it, which again can be found in the documentation.

Similarly, we can load and use other built-in modules. For example, we use the os module , which provides information about the environment and the operating system:

const os = require("os");
let userName = os.userInfo().username;
console.log(userName);

We are not limited to built-in modules and can create our own if necessary. So, in the last topic, the project consisted of an app.js file , in which a server was created that processes requests. Let’s add a new file greeting.js to the same directory and define the following code in it:

console.log("greeting module");

Let’s include our module in the app.js file:

	
const greeting = require("./greeting");

Unlike built-in modules, to include your modules, you must pass a relative path with the file name to the require function (the file extension is optional):

const greeting = require("./greeting");

Let’s run the application:

The line that is defined in the greeting.js file is output to the console.

Now let’s change the greeting.js file:

let currentDate = new Date();
module.exports.date = currentDate;

module.exports.getMessage = function(name){
let hour = currentDate.getHours();
if(hour > 16)
 return "Good Evening, " + name;
else if(hour > 10)
 return "Good Afternoon, " + name;
else
 return "Good Morning, " + name;
}

The variable is defined here currentDate. However, it is not available from outside. It is only available within this module. In order for which module variables or functions to be available, they must be defined in the module.exports object. An object module.exportsis what a function returns require()when it receives a module.

In general, the module object represents a reference to the current module, and its exports property specifies all of the module’s properties and methods that can be exported and used in other modules. For more details about the module loading definition and all its functions, see https://github.com/nodejs/node/blob/master/lib/module.js.

In particular, it defines a property date and a method getMessage that takes some parameter.

Next , let’s change the app.js file:

const os = require("os");
const greeting = require("./greeting");
// get the name of the current user
//let userName = os.userInfo().username;
let userName = "Tom";
console.log(`Request Date: ${greeting.date}`);
console.log(greeting.getMessage(userName));

All exported methods and module properties are available by name: greeting.date and greeting.getMessage().

Let’s restart the application:

Defining constructors and objects in a module
In addition to defining simple functions or properties, a module can define complex objects or constructor functions that are then used to create objects. So, let’s add a new user.js file to the project folder:

function User(name, age){
this.name = name;
this.age = age;
this.displayInfo = function(){
console.log(`Name: ${this.name} Age: ${this.age}`);
}
}
User.prototype.sayHi = function() {
console.log(`Hi, my name is ${this.name}`);
};
module.exports = User;

This defines a standard User constructor function that takes two parameters. With that, the entire module now points to this constructor function:

module.exports = User;

Let’s include and use this module in the app.js file:

const User = require("./user.js");

let tom = new User("Tom", 32);
tom.sayHi();

Let’s run the application: