KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

5 Questions

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

5 Questions

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

5 Questions

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

5 Questions

MySQL Order By

10 min

Recap Quiz

5 Questions

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

5 Questions

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

5 Questions

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

Node.jsNode.js Tutorial
Lesson

Node.js Modules

10 min reading
Free Course

Node.js Modules: CommonJS vs ES Modules Architecture

Modules are isolated code blocks that encapsulate internal variables, functions, and classes while exposing explicit interfaces to other parts of your Node.js application. Node.js supports two module systems: legacy CommonJS (CJS) and modern ECMAScript Modules (ESM).

CommonJS vs ES Modules Lifecycle Comparison

flowchart TD
    subgraph CommonJS ["CommonJS (Synchronous)"]
        C1["require() invocation"] --> C2["Load & Evaluate File Synchronously"]
        C2 --> C3["Export module.exports object"]
    end
    subgraph ESM ["ES Modules (Asynchronous / Static)"]
        E1["Static import declaration"] --> E2["Parse Module Graph & Imports"]
        E2 --> E3["Instantiate & Link Bindings"]
        E3 --> E4["Evaluate Module Top-Level Code"]
    end

Practical Code Example

Using ES Modules (.mjs or "type": "module")

// mathUtils.js - Named exports & default export
export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;

export default class Calculator {
    subtract(a, b) {
        return a - b;
    }
}
// app.js - Importing ES Modules
import Calculator, { add, multiply } from './mathUtils.js';

const calc = new Calculator();

console.log(`Sum: ${add(10, 5)}`);          // 15
console.log(`Product: ${multiply(4, 3)}`);  // 12
console.log(`Diff: ${calc.subtract(20, 8)}`);// 12

Node.js Core Prefix Namespace (node:)

Always use the explicit node: prefix when importing core modules:

import fs from 'node:fs/promises';
import path from 'node:path';
import http from 'node:http';

Best Practices & Gotchas

  • Use the node: Protocol Prefix: Always import core built-in modules using node:fs or node:path to prevent ambiguity with third-party npm packages.
  • Do Not Mix CJS and ESM Implicitly: ESM files can import CJS modules, but CJS require() cannot synchronously import ESM files; convert project bases cleanly to ESM.
  • Explicit File Extensions: ESM imports in Node.js require explicit file extensions (e.g. ./utils.js, not ./utils).

Self-Check Challenge

Create a module logger.js that exports a default Logger class and a named helper function formatTimestamp(), then import both into index.js.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum