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.

Example02
Loading editor…

📥 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')
Example03
Loading editor…

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.

Example04
Loading editor…

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

Example08
Loading editor…

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4