Nearby lessons

5 of 21

📦 Node.js – Types of Modules

📌 What are Module Types?

Types of Modules in Node.js

Node.js provides different types of modules to organize and reuse code effectively.

The main types are Local Modules, Core Modules, and Third-Party Modules.

1️⃣ Local Modules

Local modules are modules that you create yourself and use in your application.

A good example is a module library.js that can be imported in main.js.

Node.js Environment
RUNTIME ACTIVE
const greet = require('./library.js');

console.log(greet("Rajesh"));
Terminal Output
Live Output Preview

📥 Importing Local Modules

To import a local module, use the require() function with the path to your module:

  • require('./filename')
  • require('./filename.js')
  • require('./path/filename.js')
Node.js Environment
RUNTIME ACTIVE
const moduleName = require("./filename.js");
const moduleName2 = require("./filename");
Terminal Output
Live Output Preview

2️⃣ Core Modules

Core modules come with Node.js by default. You do not need to download them.

Popular core modules include fs (File System), os (Operating System), path, etc.

Node.js Environment
RUNTIME ACTIVE
const fs = require("fs");
const os = require("os");
Terminal Output
Live Output Preview

🌐 Global and Non-Global Core Modules

Global core modules like console do not require importing.

Non-global core modules like fs must be imported before use.

3️⃣ Third-Party Modules

Third-party modules are downloaded via a package manager like npm.

They are usually stored in the node_modules folder and can be installed locally or globally.

Examples include express, mongoose, react, etc.

📦 Installing Third-Party Modules

To install a third-party module:

  • Locally: npm i module_name
  • Globally: npm i module_name -g

📥 Importing Third-Party Modules

To import a third-party module, use require() with the module name:

Express.js Server
NODE RUNTIME
const express = require("express");
const mongoose = require("mongoose");
Browser Output
Server logs

🧠 Test Your Knowledge

4 Questions

Progress: 0 / 4
Keep Going!Understanding package.json & package-lock.json