KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🟢

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

Gated

Milestone Assessment

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

Gated

Milestone Assessment

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

Gated

Milestone Assessment

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js MySQL ORDER BY Clause

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

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

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

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.jsNode.js Tutorial
Lesson

Node.js Modules

10 min reading
Free Course

Node.js Modules: CommonJS vs ES Modules Explained

Node.js modules split an application into small, self-contained files where variables stay private unless explicitly exported. Breaking routing, database queries, and formatting helpers into separate modules prevents global namespace conflicts and simplifies unit testing.

CommonJS (require) vs ES Modules (import)

Node.js supports two module systems: the classic CommonJS (CJS) format used in legacy codebases and the JavaScript-standard ES Modules (ESM) format used in modern Node.js projects.

Feature CommonJS (.cjs / default) ES Modules (.mjs or "type": "module")
Import Syntax const fs = require('node:fs') import fs from 'node:fs'
Export Syntax module.exports = { fn } export function fn() {}
Loading Mode Synchronous at runtime Asynchronous static graph
Top-Level await Not supported Supported natively
Path Globals __dirname, __filename import.meta.dirname (Node 20.11+)

Writing and Importing Local Modules

In an ES module project ("type": "module" in package.json), always include the .js file extension on relative local imports (./math.js).

// tax.js (ES Module)
export function addSalesTax(amount, rate = 0.08) {
  return Number((amount * (1 + rate)).toFixed(2));
}

// checkout.js
import { addSalesTax } from './tax.js';

console.log('Total due:', addSalesTax(49.5));

In a CommonJS file, assign public functions to module.exports and load them with require('./tax').

// tax.cjs (CommonJS)
function addSalesTax(amount, rate = 0.08) {
  return Number((amount * (1 + rate)).toFixed(2));
}
module.exports = { addSalesTax };

Common Mistakes to Avoid

  • Omitting .js in ESM relative imports: Writing import { helper } from './helper' throws ERR_MODULE_NOT_FOUND in native Node.js ESM. Always include .js.
  • Reassigning exports = myFunc in CommonJS: Reassigning exports breaks the reference to module.exports and exports an empty object {}. Always assign module.exports = myFunc.
  • Forgetting node: prefix on built-in modules: Always write import path from 'node:path' so Node never accidentally resolves a malicious third-party package named path.

Frequently Asked Questions

Should new Node.js projects use CommonJS or ES Modules?

Use ES Modules ("type": "module" in package.json). ESM matches browser JavaScript and TypeScript standards, enables top-level await, and supports cleaner tree-shaking.

How do I get __dirname inside an ES module?

In Node.js 20.11+, read import.meta.dirname directly. In older Node 18/20 releases, compute it via path.dirname(fileURLToPath(import.meta.url)).

Can an ES module import a CommonJS package from npm?

Yes. ESM files can import almost any CommonJS npm package via default import (import pkg from 'cjs-pkg').

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

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Related Developer Tools & Topic Hub

Node.js Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All Node.js Articles
Deep-dive guides, tips & discussions
PreviousNode.js Get Started
Continue JourneyNext: Node.js HTTP Module

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum