What are Modules in Node.js

  • A module is a self-contained piece of code that encapsulates related functionality and can be easily reused in other parts of an application.
  • In Node.js each file is treated as a separate module.
  • Node.js uses a CommonJS-based module system, which means that each module has its own scope and can export specific functions or objects that can be used by other modules in the application.

Type of Modules

  1. Local modules: These modules are created by the developer and can be reused across different parts of an application. Custom modules can be defined in separate .js files, and can be imported into other parts of the application using the require() function.
  2. Built-in modules: These modules are built into Node.js and can be accessed without any additional installation or configuration. Examples of built-in modules include fs, http, path, and crypto.
  3. Third-party modules: These modules are created by external developers and can be installed using Node.js package manager (NPM) or Yarn. Third-party modules are published on NPM, which is a package registry for Node.js. Examples of popular third-party modules include Express, Mongoose, and Socket.IO.