What are modules in Node.js, and what is the difference between CommonJS and ES Modules?
Updated Feb 20, 2026
Short answer
Modules are reusable units of code that let Node.js applications split functionality into separate files. Node.js supports two module systems: CommonJS, the older require() based system, and ES Modules, the modern JavaScript standard using import and export. The main differences are syntax, loading behavior, and ecosystem compatibility.
Deep explanation
What are modules in Node.js?
A module is a self-contained piece of code that exposes selected functionality and hides its internal implementation. Instead of placing an entire application in one large file, developers divide it into smaller files that can be imported and reused.
Node.js treats every file as a module. A module can contain:
- Functions and classes
- Constants and configuration values
- Database logic
- API handlers
- Utility helpers
A module usually has two responsibilities:
- Define private implementation details.
2.…
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro