Paramclasses Academy - Blog

Tutorials Details

←
Post 6 of 40
→

CommonJS vs ES Modules in Node.js: Key Differences

CommonJS vs ES Modules in Node.js: Key Differences

October 29, 2026

✦ CommonJS vs ES Modules in Node.js: Key Differences

Node.js supports two major module systems: CommonJS (CJS) and ECMAScript Modules (ESM). Both systems help developers divide JavaScript code into reusable files, import functions, and organize large applications.

However, they use different syntax and have differences in how modules are loaded and configured. Understanding these differences is important when developing Node.js applications, working with npm packages, or building backend APIs using Express.js.

In this tutorial by Param Classes, you will learn CommonJS and ES Modules with practical examples, their key differences, how to configure them, and when to use each module system.

✦ What Is CommonJS in Node.js?

CommonJS is a module system widely used in Node.js applications. It uses require() to import code and module.exports to export functions, objects, or other values.

It is commonly found in existing Node.js projects and many backend tutorials.

❖ Example of CommonJS

Create a file named calculator.js:

function add(a, b) {
    return a + b;
}

function subtract(a, b) {
    return a - b;
}

module.exports = {
    add,
    subtract
};

In this example, two functions are created and exported using module.exports.

Now create another file named app.js:

const calculator = require('./calculator');

console.log(calculator.add(10, 5));
console.log(calculator.subtract(10, 5));

Output:

15
5

The statement require('./calculator') loads the local module, and the returned object provides access to its exported functions.

❖ CommonJS Key Features

  • Uses require() to import modules.

  • Uses module.exports or exports to expose functionality.

  • Is the default module system for Node.js .js files when no other module configuration applies.

  • Is commonly used in existing Node.js projects.

  • Supports loading modules through the CommonJS module system.

✦ What Are ES Modules in Node.js?

ES Modules, also called ESM, are the standard JavaScript module system. They use import and export keywords to share functionality between files.

ES Modules are supported by modern JavaScript environments, including Node.js.

❖ Example of ES Modules

Create a file named calculator.js:

export function add(a, b) {
    return a + b;
}

export function subtract(a, b) {
    return a - b;
}

Here, the export keyword makes the functions available to other modules.

Now create app.js:

import { add, subtract } from './calculator.js';

console.log(add(10, 5));
console.log(subtract(10, 5));

Output:

15
5

Notice that the relative import includes the .js extension, which is the usual requirement for relative imports in Node.js ES Modules.

❖ How to Enable ES Modules

There are two common ways to identify JavaScript files as ES Modules in Node.js.

Method 1: Add "type": "module" to package.json

{
    "type": "module"
}

With this setting, .js files in the relevant package scope are interpreted as ES Modules.

Method 2: Use the .mjs Extension

You can name your file app.mjs and run it with:

node app.mjs

The .mjs extension identifies the file as an ES Module.

✦ CommonJS vs ES Modules: Key Differences

Although both systems allow code reuse, they differ in syntax, loading behavior, and configuration.

Feature CommonJS ES Modules
Import syntax require() import
Export syntax module.exports export
Typical file extension .cjs or configured .js .mjs or configured .js
Loading model CommonJS loading system Standard JavaScript module system
Module structure Exports through the module object Static import/export declarations
Dynamic loading require() can load modules conditionally import() supports dynamic loading
Browser support Primarily a Node.js ecosystem system Supported by modern browsers and Node.js
Typical use Existing Node.js applications and compatible packages Modern JavaScript applications and new projects

Important: Both module systems remain useful. The correct choice depends on your project configuration, dependencies, and compatibility requirements.

✦ Understanding require() vs import

The biggest visible difference between CommonJS and ES Modules is how you import code.

❖ CommonJS Import

const fs = require('fs');

console.log('File System module loaded');

This example loads Node.js's built-in File System module using CommonJS syntax.

❖ ES Module Import

import fs from 'node:fs';

console.log('File System module loaded');

This example imports the same built-in module using ES Module syntax.

The node: prefix explicitly identifies the module as a Node.js built-in module.

❖ Named Imports in ES Modules

You can import only the functions you need.

For example, create math.js:

export function multiply(a, b) {
    return a * b;
}

export function divide(a, b) {
    return a / b;
}

Then import the functions:

import { multiply, divide } from './math.js';

console.log(multiply(4, 5));
console.log(divide(20, 4));

Output:

20
5

This approach can make dependencies clear and help organize larger applications.

✦ Default Exports vs CommonJS Exports

ES Modules support default exports, which provide another way to export a value.

❖ ES Modules Default Export

Create greeting.js:

export default function greet(name) {
    return `Hello, ${name}!`;
}

Create app.js:

import greet from './greeting.js';

console.log(greet('Student'));

Output:

Hello, Student!

In CommonJS, a similar pattern uses module.exports:

function greet(name) {
    return `Hello, ${name}!`;
}

module.exports = greet;

Import it using:

const greet = require('./greeting');

console.log(greet('Student'));

Both examples expose a function, but they use different module systems. Keep the syntax consistent with the configuration of your project.

✦ Can You Use CommonJS and ES Modules in the Same Project?

Yes. Node.js can support both module systems in one project, but you must follow the appropriate file and package configuration rules.

For example:

  • .cjs files are treated as CommonJS.

  • .mjs files are treated as ES Modules.

  • .js files are generally interpreted according to the nearest relevant package.json configuration.

You can use import() to dynamically load a module when needed. CommonJS files can also load compatible ES Modules using import().

However, mixing the two systems without understanding their differences can lead to errors. For beginners, using one consistent module system for application code is usually easier.

✦ Common Errors and How to Fix Them

❖ 1. require is not defined

This can occur when you use require() inside an ES Module.

Incorrect in an ES Module:

const fs = require('fs');

Correct ES Module syntax:

import fs from 'node:fs';

Alternatively, use CommonJS syntax in a .cjs file or in a .js file configured for CommonJS.

❖ 2. Cannot use import statement outside a module

This commonly occurs when Node.js interprets your file as CommonJS but your code uses ES Module syntax.

Solution: Add "type": "module" to the appropriate package.json, rename the file to .mjs, or change the code to CommonJS syntax.

❖ 3. Module Not Found

If Node.js cannot find a local module, check the file name and import path.

For example:

import { add } from './calculator.js';

Ensure that calculator.js exists in the correct location and that its exported function is named add.

✦ Which Module System Should Beginners Learn?

Both systems are worth understanding because you may encounter either one while learning Node.js or working on existing projects.

Choose CommonJS when maintaining a project that already uses require() and module.exports, or when a dependency or project setup requires it.

Choose ES Modules when working with modern JavaScript syntax, standard import and export statements, or a project configured for ESM.

If you are starting a new project, ES Modules are a good choice when your dependencies and deployment environment support them. Learning CommonJS as well will help you understand older tutorials and existing codebases.

✦ Frequently Asked Questions (FAQs)

❖ 1. What is the main difference between CommonJS and ES Modules?

CommonJS uses require() and module.exports, whereas ES Modules use import and export.

❖ 2. Which module system does Node.js support?

Node.js supports both CommonJS and ES Modules.

❖ 3. What does "type": "module" do?

It tells Node.js to interpret .js files within the relevant package scope as ES Modules.

❖ 4. What are .cjs and .mjs files?

.cjs identifies a CommonJS file, while .mjs identifies an ES Module.

❖ 5. Can I use require() and import together?

Both systems can coexist in a project, but their usage depends on file configuration and compatibility. Dynamic import() is supported in both module contexts.

❖ 6. Which module system is better for beginners?

ES Modules are a useful starting point for modern JavaScript development, but learning CommonJS is also important for understanding existing Node.js applications.

✦ Conclusion

CommonJS and ES Modules are two important ways to organize JavaScript code in Node.js. CommonJS relies on require() and module.exports, while ES Modules use import and export.

Understanding both systems will help you build structured applications, troubleshoot module-related errors, work with npm packages, and develop backend projects more confidently.

At Param Classes, learners can strengthen their programming skills through practical examples and step-by-step tutorials covering Node.js, JavaScript, backend development, and database integration.

Visit Param Classes for learning resources and ParamWebInfo for website development and digital solutions.


✦ Related Topics

  • What Is Node.js? Features, Benefits and Uses for Beginners

  • Node.js Modules Explained: Built-In, Local and Third-Party Modules

  • Understanding npm: Install, Update and Manage Packages

  • How to Create and Use a package.json File in Node.js

  • Express.js Tutorial: Create Your First Node.js Web Application

 

←
Post 6 of 40
→