MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
CommonJS vs ES Modules & Package.json Dependency Management0%
Study Materials
Node.js & Express.jsProgress 0%
1.1Node.js Architecture, V8 Engine & Single-Threaded Event Loop1.2Call Stack, Callback Queue, Microtask Queue & Libuv Workers1.3Non-Blocking Asynchronous Code with Promises and Async/Await
2.1CommonJS vs ES Modules & Package.json Dependency Management2.2File System Operations: Sync, Async & Promises API (fs/promises)2.3EventEmitter Pattern & Custom Event Architecture2.4Buffers and Streams: Readable, Writable & Pipeline for Large Files
HomeTutorialsNode.js & Express.jsCommonJS vs ES Modules & Package.json Dependency Management
File System Operations: Sync, Async & Promises API (fs/promises)

CommonJS vs ES Modules & Package.json Dependency Management

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Node.js has two module systems: the legacy CommonJS (CJS) standard and the official ECMAScript ES Modules (ESM) standard. Understanding the differences, interoperability, and modern NPM dependency management is essential for all professional backend developers.


1. CommonJS (CJS) vs ES Modules (ESM)

FeatureCommonJS (CJS)ES Modules (ESM)
Syntaxconst x = require('./x') / module.exports = ...import x from './x.js' / export default ...
Loading MechanismSynchronous, runtime evaluationAsynchronous, static parsing phase
Top-Level AwaitSupported only in limited wrapper contextsSupported natively at top-level
File Extension.cjs or default .js.mjs or .js with "type": "module" in package.json
Dynamic ImportsDynamic require() allowed anywhereimport() returns a Promise
Special Variables__dirname, __filename availableUse import.meta.url and fileURLToPath

Enabling ES Modules in Node.js

To use modern ESM across your Node.js project, add "type": "module" to your package.json:

1{
2 "name": "backend-api",
3 "version": "1.0.0",
4 "type": "module",
5 "scripts": {
6 "start": "node src/server.js"
7 }
8}

Resolving __dirname in ES Modules

1import { fileURLToPath } from 'node:url';
2import { dirname, join } from 'node:path';
3 
4const __filename = fileURLToPath(import.meta.url);
5const __dirname = dirname(__filename);
6 
7console.log('Current directory:', __dirname);

2. Package.json: Dependencies vs DevDependencies

Managing dependencies correctly is crucial for keeping production Docker images slim and secure:

1{
2 "dependencies": {
3 "express": "^4.21.0",
4 "cors": "^2.8.5",
5 "dotenv": "^16.4.5"
6 },
7 "devDependencies": {
8 "nodemon": "^3.1.4",
9 "eslint": "^9.10.0",
10 "jest": "^29.7.0"
11 }
12}
  • dependencies: Essential runtime libraries required to serve production traffic (Express, database drivers, JWT).
  • devDependencies: Tools needed strictly for local development and CI/CD pipelines (test runners, linters, TypeScript compiler, Nodemon).
  • In production deployment, install only runtime libraries: npm ci --omit=dev.

Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
How do you configure a Node.js project so that all .js files are treated as ES Modules (ESM) by default?
Question 2
Which global variables are available natively in CommonJS (CJS) but undefined in ES Modules (ESM)?
Question 3
What is the semantic meaning of the caret symbol (^) in a dependency version declaration like "express": "^4.18.2"?
Question 4
What command installs strictly the exact dependencies recorded in package-lock.json without modifying the lockfile, ideal for production CI/CD?
Question 5
Why should development utilities like nodemon and jest be installed under devDependencies rather than dependencies?

Next Lesson

File System Operations: Sync, Async & Promises API (fs/promises)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Non-Blocking Asynchronous Code with Promises and Async/AwaitFile System Operations: Sync, Async & Promises API (fs/promises)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Node.js & Express.js with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext