DEV Community

Cover image for Getting started with Node.js modules
Christopher Glikpo
Christopher Glikpo

Posted on

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.

What is a module?

A module is just a file.One script is one module.As simple as that.

Modules are of three types:

  • Core Modules
  • local Modules
  • Third-party modules or community based modules

Let’s now explain each in little more details :

Core Modules or Built-in Modules

  • 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:

  • fs: Allows you to manipulate (create/read/write) files and directories.
  • path: utilities to work with files and directories paths.
  • http: create HTTP servers and clients for web development.
  • url: utilities for parsing URLs and extracting elements from it.

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');
Enter fullscreen mode Exit fullscreen mode

What is Require function?

Require are used to import modules, JSON, and local files. Modules can be imported from node_modules.

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');
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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().

Local Module

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.

Writing Simple Module

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;
};
Enter fullscreen mode Exit fullscreen mode

Since this file provides attributes to the outer world via exports,another file can use its exported functionality using require() function.

Loading Local Module

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));
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Third-party modules or community based modules

NPM modules are 3rd-party modules that you can use after you install them. To name a few:

  • lodash: a collection of utility functions for manipulating arrays, objects, and strings.
  • request: HTTP client simpler to use than the built-in http module.
  • express: HTTP server for building websites and API. Again, simpler to use than the built-in http module.

These you have to install them first, like this:

 npm install express
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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 like my work, please consider
Buy me a coffee
so that I can bring more projects, more articles for you

If you want to learn more about Web Development, feel free to follow me on Youtube!

Top comments (0)