Node.js Tutorial 0/203 lessons ~6 min read Lesson 16

    CommonJS Modules

    Use require, module.exports, and exports, and understand wrapping and load order.

    Course progress0%
    Focus
    20 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Learning Objectives

    After completing this lesson, you will be able to:

    • Explain CommonJS Modules using require() resolution plus module cache.
    • Apply it to maintaining existing CJS services without confusing Node.js with Express or TypeScript.
    • Recognize and correct this failure mode: mutating exports after consumers cached the object without a plan.
    • Decide when CommonJS Modules is the right tool: prefer a single module.exports assignment.
    • Describe the event-loop and I/O implications of this topic.
    • Explain CommonJS Modules in terms of the Node.js runtime, not as a JavaScript language feature.
    • Describe what V8, libuv, and the operating system each contribute.
    • Identify whether the work is I/O-bound or CPU-bound.
    • Handle operational errors without hiding programmer defects.
    • Keep Express, TypeScript, and Node.js responsibilities distinct.
    • Validate untrusted input at runtime before it reaches domain logic.
    • Reason about event-loop delay, memory, and backpressure.
    • Apply CommonJS Modules to a TechLearningPro backend use case.
    • State when not to use this technique.
    • Defend the design in an interview with trade-offs.

    Introduction

    A TechLearningPro backend must support maintaining existing CJS services. Treating Node.js as "just JavaScript on a server" hides runtime, I/O, and security costs. The team needs a design that is explicit about require() resolution plus module cache and honest about what the process can and cannot do.

    Node.js is a JavaScript runtime, not a programming language. This lesson treats CommonJS Modules as an engineering decision: what the runtime does, how the event loop is involved, and how the idea appears in a production TechLearningPro backend.

    What Is This Concept?

    In simple language: CommonJS loads modules synchronously and caches the exported object.

    Professional explanation: CommonJS Modules is a Node.js runtime concern based on require() resolution plus module cache. It helps engineers implement maintaining existing CJS services while remaining clear that Node.js executes JavaScript through V8 and reaches the operating system through libuv and Node.js APIs.

    Why Do We Need It?

    text
    Without CommonJS Modules
    Unclear runtime behavior or a fragile backend
    Node.js solution
    Predictable I/O, clearer ownership, safer operations
    • It makes maintaining existing CJS services an explicit backend responsibility.
    • It prevents mixing browser JavaScript assumptions with server I/O.
    • It gives reviewers a vocabulary for event-loop and failure behavior.
    • It supports the key decision: prefer a single module.exports assignment.
    • It keeps framework and language features from being mistaken for the runtime.

    Real-World Analogy

    A photocopier that keeps one master copy after the first print.

    How It Works Internally

    Runtime behavior

    At runtime, CommonJS Modules follows ordinary JavaScript semantics inside V8, plus any Node.js or operating-system APIs involved in require() resolution plus module cache. Types and comments do not execute.

    Event loop implications

    If CommonJS Modules performs I/O, libuv schedules the work and the callback or Promise continuation later returns to the event loop. If it performs heavy CPU work on the main thread, timers, I/O callbacks, and incoming HTTP work wait.

    Critical distinction: JavaScript is the language. V8 executes it. Node.js adds the event loop, libuv, and OS APIs. Express is a framework on top of Node.js HTTP. TypeScript types do not validate runtime input.
    1. 1. Name the invariant: maintaining existing CJS services.
    2. 2. Identify the Node.js mechanism: require() resolution plus module cache.
    3. 3. Separate main-thread JavaScript from libuv / OS work.
    4. 4. Define success, timeout, and failure paths.
    5. 5. Validate untrusted input before domain logic.
    6. 6. Add observability (logs, metrics, or traces) at the boundary.

    Architecture

    text
    JavaScript
    V8 (execute CommonJS Modules)
    Node.js APIs / libuv
    Operating system / thread pool
    Callback / microtask queues
    Event loop resumes the application
    TechLearningPro response or side effect

    Code Examples

    Basic Example: Smallest useful example

    This isolates the essential behavior of CommonJS Modules.

    js
    // CommonJS Modules — smallest useful Node.js example
    import { createRequire } from "node:module";
    console.log("runtime", process.release.name);
    console.log("pid", process.pid);

    Intermediate Example: Realistic service usage

    This applies the idea to maintaining existing CJS services.

    js
    // CommonJS Modules — TechLearningPro service sketch
    export async function handlecommonjsmodules(input) {
    if (input == null || typeof input !== "object") {
    throw new Error("Untrusted input must be validated first");
    }
    return { ok: true, topic: "CommonJS Modules" };
    }

    Advanced Example: Production-oriented design

    This version makes the trade-off—prefer a single module.exports assignment—explicit.

    js
    // CommonJS Modules — production-oriented composition
    export function createcommonjsmodulesHandler({ clock, logger }) {
    return async function handler(request) {
    const started = clock.now();
    try {
    return { status: 200, body: { topic: "CommonJS Modules" } };
    } finally {
    logger.info({ ms: clock.now() - started, topic: "commonjs-modules" });
    }
    };
    }

    Enterprise Example

    TechLearningPro uses CommonJS Modules while implementing maintaining existing CJS services. The HTTP adapter stays thin, the application service owns the use case, and I/O is isolated. Reviewers can tell Node.js runtime behavior from Express helpers and from TypeScript types.

    text
    Student
    API Gateway
    Node.js service
    ├── Router / HTTP adapter
    ├── Authn / Authz
    ├── Application service
    └── Repository / client
    Database / Queue / Cache

    Deep Dive

    require() resolution plus module cache matters because it determines whether work is scheduled, blocked, or offloaded.

    The principal design risk is mutating exports after consumers cached the object without a plan. A strong design keeps the event loop free, timeouts explicit, and diagnostics readable.

    CommonJS Modules ends at a trust boundary. HTTP bodies, files, environment variables, and messages start untrusted.

    The governing trade-off is prefer a single module.exports assignment. Prefer the least infrastructure that solves a measured problem.

    Common Mistakes

    For each mistake, name the false assumption and replace it with an explicit runtime contract:

    1. 1. Treating CommonJS Modules as a JavaScript language feature instead of a Node.js runtime concern.
    2. 2. Assuming Node.js is secure by default.
    3. 3. Ignoring the central pitfall: mutating exports after consumers cached the object without a plan.
    4. 4. Blocking the event loop with CPU-heavy or synchronous I/O work.
    5. 5. Presenting Express middleware as a Node.js core API.
    6. 6. Trusting TypeScript types as runtime validation.
    7. 7. Swallowing Promise rejections or using empty catch blocks.
    8. 8. Adding clustering or worker threads before measuring the bottleneck.
    9. 9. Logging secrets, tokens, or raw request bodies.
    10. 10. Repeating an earlier lesson instead of composing the next layer.

    Best Practices

    • Keep the main thread free of unnecessary CPU work.
    • Prefer async I/O over synchronous fs and crypto in request paths.
    • Validate every external payload at the boundary.
    • Use structured errors with request or correlation IDs.
    • Load configuration from the environment, not hardcoded secrets.
    • Separate Node.js platform setup from application services.
    • Distinguish operational errors from programmer errors.
    • Add timeouts to outbound HTTP, database, and queue calls.
    • Treat Express as optional infrastructure, not the domain model.
    • Use TypeScript for contracts; use runtime validators for input.
    • Watch event-loop delay and memory in production.
    • Keep dependencies minimal and audited.
    • Make background jobs idempotent.
    • Shut down HTTP servers and open handles on SIGTERM.
    • Document when not to use the technique.
    • Revisit the decision: prefer a single module.exports assignment.

    Performance

    Node.js performance work starts with the event loop. Blocking the main thread delays every concurrent request. Measure before introducing clustering, worker threads, or extra infrastructure.

    • CommonJS Modules is only as fast as the slowest I/O or CPU step on the path.
    • Profile event-loop delay before blaming Node.js itself.
    • Streams and backpressure matter when payloads are large.
    • Do not enable cluster or worker_threads as a default recipe.

    Security

    Node.js is not secure by default. Security depends on application architecture, dependencies, configuration, validation, authentication, authorization, and deployment.

    • Validate and authorize independently of UI or framework checks.
    • Never execute unsanitized paths, commands, or query fragments.
    • Store secrets in the environment or a secret manager.
    • Keep dependency and supply-chain reviews part of delivery.
    • Use CommonJS Modules to improve operations, not as a substitute for policy.

    Real-World Architecture

    Place CommonJS Modules in the narrowest layer that owns its invariant. HTTP adapters translate protocol; services coordinate use cases; repositories talk to data stores; the composition root wires Node.js process concerns.

    Interview Questions & Answers

    Beginner

    1What problem does CommonJS Modules solve?+
    It supports maintaining existing CJS services using require() resolution plus module cache. The value is explicit runtime behavior, not a new JavaScript syntax feature.
    2Where does this run?+
    Inside the Node.js process: V8 executes JavaScript, and Node.js APIs plus libuv reach the operating system. It is not a browser Web API unless separately noted.
    3Is CommonJS Modules part of the JavaScript language?+
    No. JavaScript is the language. Node.js is a runtime that executes JavaScript outside the browser using V8, plus operating-system APIs provided by the Node.js platform. Distinguish language features from runtime APIs.
    4How does this topic differ from Express.js?+
    Express is a third-party web framework that sits on top of Node.js HTTP primitives. CommonJS Modules should be explained in Node.js terms first. If a design only works because Express middleware exists, say so instead of presenting it as a Node.js language feature.
    5What happens on the event loop when this feature is used?+
    Most Node.js I/O is scheduled through libuv and later resumed on the event loop. CPU-heavy work on the main thread still blocks timers, I/O callbacks, and incoming requests. Know whether CommonJS Modules is I/O-bound, CPU-bound, or mixed.

    Intermediate

    1How would you test this in a Node.js service?+
    Unit-test the pure logic, integration-test the I/O boundary, and assert both success and failure paths including timeouts.
    2When would you avoid CommonJS Modules?+
    Avoid it when a simpler Node.js primitive already solves the problem, when the work is a poor fit for a single-threaded event loop, or when the team would be adopting a library for ceremony rather than an operational need.
    3How should errors be handled around CommonJS Modules?+
    Treat operational failures as expected (timeouts, missing files, downstream 5xx) and programmer errors as defects. Never swallow Promise rejections. Convert unknown failures at the boundary into structured errors with a request identifier.
    4Does TypeScript make CommonJS Modules safe at runtime?+
    No. TypeScript types are erased. HTTP bodies, query strings, environment variables, and queue payloads remain untrusted until validated by runtime code.

    Senior

    1When would you reject this design in review?+
    When the pitfall appears: mutating exports after consumers cached the object without a plan. Prefer a simpler Node.js primitive if the extra machinery does not protect a real invariant.
    2How would you load-test a TechLearningPro service that depends on CommonJS Modules?+
    Measure event-loop delay, request latency percentiles, memory growth, and downstream saturation separately. Do not recommend clustering or worker threads until a profile shows the bottleneck is CPU on the main thread rather than I/O or a database.
    3What production failure mode is most common here?+
    The usual failure is an implicit assumption about asynchrony or trust. Add timeouts, backpressure, structured logs, and a clear ownership boundary before adding more infrastructure.
    4How do you keep this from becoming a God module?+
    Keep HTTP adapters thin, put business rules in services, isolate I/O behind repositories or clients, and keep Node.js platform concerns (signals, process, config) at the composition root.

    Architect

    1How should this live on a platform?+
    Own it in one service, validate at trust boundaries, observe it, and accept the trade-off: prefer a single module.exports assignment.
    2How should CommonJS Modules sit in a multi-service TechLearningPro backend?+
    Own the invariant in one service, expose a small contract, validate at every trust boundary, and choose sync HTTP versus async messages based on coupling and failure isolation—not fashion.
    3What is the security stance for this area?+
    Node.js is not secure by default. Security depends on validation, authentication, authorization, dependency hygiene, secrets management, and deployment. CommonJS Modules can hide or expose risk, but it never replaces backend policy.
    4How would you evolve this design over years?+
    Version public contracts, keep the process stateless where possible, make background work idempotent, and measure before introducing workers, clusters, or extra brokers. Complexity must pay for a named operational problem.

    Practical Exercise

    Problem: Write a CJS utility and consume it from another file.

    Difficulty: Beginner

    Requirements

    • Use async I/O on the request path unless the lesson is about a blocking primitive.
    • Validate untrusted input.
    • Show a timeout or failure path.
    • Do not treat Express or TypeScript as Node.js itself.

    Expected behavior: A small TechLearningPro module that uses CommonJS Modules to support maintaining existing CJS services and documents the runtime boundary.

    Hints

    • Start from require() resolution plus module cache.
    • Watch for mutating exports after consumers cached the object without a plan.
    • Ask whether the work belongs on the event loop or off it.

    The full solution is intentionally withheld. Implement the contract, then review failure modes aloud.

    Key Takeaways

    • CommonJS Modules models maintaining existing CJS services through require() resolution plus module cache.
    • Node.js is a runtime; JavaScript is the language.
    • V8 executes code; libuv and the OS perform most I/O.
    • The main hazard is mutating exports after consumers cached the object without a plan.
    • The key trade-off is prefer a single module.exports assignment.
    • Express and TypeScript are not Node.js.
    • Types do not validate runtime input.
    • Do not block the event loop without a measured reason.
    • Security is an application and operations property.
    • Compose the next lesson instead of reteaching this contract.

    Summary

    CommonJS Modules gives TechLearningPro a precise way to implement maintaining existing CJS services through require() resolution plus module cache. Used with honest event-loop reasoning, boundary validation, and clear ownership, it improves backend change safety without pretending Node.js is a language or a security product.

    Next Lesson Preview

    Next, study ES Modules. The next lesson extends this Node.js foundation with the next production concern.

    Ready to mark this lesson complete?Track your journey across the entire course.