Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NODENode.js

    Runtime and core tooling

    • What is Node.js?
    • CommonJS and ES modules
    • npm and package.json
    • process and command-line apps

    Async I/O and events

    • Event loop: callbacks to async/await
    • Files and paths with fs/promises
    • EventEmitter
    • Streams and backpressure

    HTTP and APIs

    • Your first HTTP server
    • Routing and JSON bodies
    • Build a tiny router and middleware chain
    • REST: resources and status codes
    • Validation and error handling

    Production: security, testing and deployment

    • Authentication basics with crypto
    • Configuration and environment secrets
    • Testing with assert and node:test
    • API security fundamentals
    • Project: tasks REST API and deployment
    Node.js

    Runtime and core tooling · Lesson 2 of 18

    CommonJS and ES modules

    Split programs behind clear contracts and understand require/export and import/export.

    11 minutes2 games

    A module is a file with private scope and a public contract. CommonJS uses require() and module.exports; ESM uses import and export, enabled through .mjs or "type": "module". A project should be explicit about its choice.

    math.js hides details

    exports public contract

    app.js imports

    module cache

    Node.js
    const path = require("node:path")
    const calculator = {
      add(a, b) { return a + b },
      label(file) { return path.basename(file) }
    }
    module.exports = calculator
    console.log(calculator.add(4, 7), calculator.label("/tmp/app.js"))

    Node caches a module after its first require, so top-level code usually runs once. Export a small API rather than all internals, and avoid circular dependencies because they can expose partially initialized exports.

    JavaScript
    // ESM version (shown only; runnable lessons use CommonJS)
    // math.mjs
    export const add = (a, b) => a + b
    
    // app.mjs
    import { add } from "./math.mjs"
    console.log(add(4, 7))

    Important

    exports = value does not replace the exported value; it only reassigns a local variable. Use module.exports = value, or attach properties such as exports.add = add.

    Quick question

    In CommonJS, how do you export one function directly?

    Interview question

    What is the main practical difference between CommonJS and ESM?

    Exercise

    Complete an exported object with double, then print double(6).

    Expected output: 12

    Node.js
    const tools = {
    
    }
    module.exports = tools
    console.log(tools.double(6))

    Learn more from

    • Node.js: CommonJS modulesnodejs.org
    • MDN: JavaScript modulesdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousWhat is Node.js?Nextnpm and package.json
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.