19
Getting started with Node.js modules
Modules are an important topic to grasp when working with Node.js applications.In this post, we cover Node modules.
A module is just a file.One script is one module.As simple as that.
- Core Modules
- local Modules
- Third-party modules or community based modules
Let’s now explain each in little more details :
- Core Modules:Nodejs has many built-in modules that are part of the platform and comes with Node.js installation.
Some of the most used core modules are:
These you don’t have to install it, you can import them and use them in your programs.These modules can be loaded into the program by using the require function.
Syntax:
const module_name=require('module_name');
NodeJS provides the require function, whose job is to load modules and give you access to their exports.
You don't have to call the variable http, you can do
const myvariable = require('http');
and use myvariable instead, but convention is that you'd use the module's name, or if only using one part of a module, to use the name of that part as defined by the module's documentation.
The following example shows how to create a web server with the Node.js http module.
//Example: Load and Use Core http Module
const http = require('http');// 1. Import Node.js core module
const server = http.createServer(function(req, res){// 2. Creating Server
//handle incoming requests here.
});
server.listen(5000); // 3. Listen for any incoming reuqests.
In the above example, require() function returns an object because http module returns its functionality as an object, you can then use its properties and methods using dot notation e.g. http.createServer().
In a Nodejs application, a local module is a module that is created locally. Custom or user-defined modules are other names for it. Local modules, in other words, are mostly utilized for individual projects that are stored locally within project folders.
So these modules contain different functionalities of your application that are separately available in files and folders.
Let's create a simple calculating module that calculates various operations.
In Node.js, module should be placed in a separate JavaScript file. So, create a calc.js file and write the following code in it.
Filename:calc.js
exports.add=function(x,y){
return x+y;
};
exports.sub=function(x,y){
return x-y;
};
exports.multi=function(x,y){
return x*y;
};
exports.div=function(x,y){
return x/y;
};
Since this file provides attributes to the outer world via exports,another file can use its exported functionality using require() function.
To use local modules in your application, you need to load it using require() function in the same way as core module. However, you need to specify the path of JavaScript file of the module.
The following example demonstrates how to use the above calculating module contained in calc.js.
Filename:app.js
let calculator=require('./calc');
let x=10,y=20;
console.log("Addition of 10 and 20 is "+calculator.add(x,y));
In the above example, app.js is using calculator module.First, it loads the calculating module using require() function and specified path where calculating module is stored. Calculating module is contained in calc.js file in the root folder. So, we have specified the path './calc.js' in the require() function. The '.' denotes a root folder.
NB
:You must specify ./ as a path of the root folder to import a local module.However,you don't need to specify the path to import Node.js core modules.
The require()
function returns a calculator object because calculating module exposes an object in calc.js
using module.exports or exports. So now you can use calculating module as an object and call any of its function using dot notation e.g calculator.add(x,y)
or calculator.sub(x,y)
or calculator.div(x,y)
Run app.js file using the following command:
node app
Addition of 10 and 20 is 30
NPM modules are 3rd-party modules that you can use after you install them. To name a few:
These you have to install them first, like this:
npm install express
and then you can reference them like built-in modules, but this time they are going to be served from the node_modules folder that contains all the 3rd-party libraries.
const express = require('express');
If you want to watch the NODEJS VIDEO feel free to click on the link to watch it.
If you've reached this point, thank you very much. I hope that this tutorial has been helpful for you and I'll see you all in the next.
If you want to learn more about Web Development, feel free to follow me on Youtube!
19