Modules in nodejs

Node.js uses a module system to organize code into reusable files. CommonJS (require/module.exports) is the traditional approach, while ES Modules (import/export) are the modern standard. Each file is treated as a separate module with its own scope.

Example

// math.js (CommonJS)
module.exports = {
  add: (a, b) => a + b,
  multiply: (a, b) => a * b,
};

// app.js
const { add, multiply } = require('./math');

Modules encapsulate related functionality and expose only what's needed through exports.