juniorNode.js

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:

  1. Define private implementation details.

2.…

Unlock with a Pro subscription to view this section.

View pricing

Real-world example

No real-world example available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Common mistakes

No common mistakes listed yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Follow-up questions

No follow-up questions available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

More Node.js interview questions

View all →