Node modules are what I would call separation of concerns in javascript. Come to think of in modules in NodeJS are either simple or complex functions arranged in javascript files which can be reused over and over again.
Node modules are divided into 3 actually, we have
Core Modules
Local Modules
3rd party Modules
Core Modules
Let's try and understand core modules in Nodejs as in the inbuilt modules which come with node, and to make use of the core modules in node all we have to do it's just to import them. Core modules in Node are as follows:
http: To make Node.js act as an HTTP server. This can be imported using this syntax. Note: You can name the variable as you wish.
const http = require('http');
https: To make Node.js act as an HTTPS server.
const https = require('https');
path: To handle file paths.
const path = require('path');
fs: To handle the file system.
const fs = require('fs');
Local Modules
Now, let's see local modules as modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders. You can also package it and distribute it via NPM so that Node.js community can use it. Below is an example of a local module I created which returns a 404 page when a certain route does not exist.
exports.get404page = (req, res, next) => {
res.status(404).render('404', {pageTitle: 'Page Not Found', path: ''})
}
3rd Party Modules
3rd party modules are node modules that are pulled in using the Node Package Manager (NPM).
Node Package Manager (NPM) is a command-line tool that installs, updates or uninstalls Node.js packages in your application. It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository.
Official website: https://www.npmjs.com
Examples 3rd party modules in node are:
Express: Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications and can be installed to our node project using;
npm install express --save
Nodemon: Nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
npm install --save-dev nodemon
Body parser: Parse incoming request bodies in a middleware before your handlers.
npm install body-parser
There are a lot of 3rd party modules in Nodejs, they are what makes the development, deployment and the production process of Nodejs a breeze. Do check out the npm official website and know about more 3rd party packages in Node.