CommonJS and ESM are two ways to organize and share code in Node.js. They help your code talk to other files or libraries.
CommonJS vs ESM differences in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
CommonJS: const module = require('module') module.exports = value ESM: import module from 'module' export default value
CommonJS uses require() and module.exports.
ESM uses import and export keywords.
Examples
Node.js
const fs = require('fs') const data = fs.readFileSync('file.txt', 'utf8') module.exports = data
Node.js
import fs from 'fs' const data = fs.readFileSync('file.txt', 'utf8') export default data
Node.js
const add = (a, b) => a + b
module.exports = { add }Node.js
export function add(a, b) {
return a + b
}Sample Program
This shows a CommonJS module exporting a function, and an ESM module importing and using it.
Note: Node.js requires special setup to mix CommonJS and ESM files.
Node.js
// CommonJS module: math.js
const multiply = (x, y) => x * y
module.exports = { multiply }
// ESM module: main.mjs
import { multiply } from './math.js'
console.log(multiply(3, 4))Important Notes
CommonJS loads modules synchronously, ESM loads them asynchronously.
ESM supports static analysis and tree shaking for smaller bundles.
Node.js treats files with .mjs as ESM and .cjs as CommonJS by default.
Summary
CommonJS uses require and module.exports, ESM uses import and export.
ESM is the modern standard and supports better optimization.
Choose based on your project needs and Node.js version compatibility.
Practice
1. Which syntax is used to import modules in CommonJS in Node.js?
easy
Solution
Step 1: Understand CommonJS import syntax
CommonJS uses therequirefunction to import modules.Step 2: Compare with other options
Options A, B, and C are not valid CommonJS import syntax; B is ESM syntax.Final Answer:
const module = require('module') -> Option AQuick Check:
CommonJS imports use require() [OK]
Hint: CommonJS uses require(), ESM uses import [OK]
Common Mistakes:
- Confusing ESM import syntax with CommonJS
- Using import in CommonJS files
- Trying to use module.import() which doesn't exist
2. Which of the following is the correct way to export a function in ESM syntax?
easy
Solution
Step 1: Identify ESM export syntax
ESM usesexport defaultto export a default function or value.Step 2: Eliminate CommonJS and invalid options
module.exports = function() {}and C are CommonJS exports; D is invalid syntax.Final Answer:
export default function() {} -> Option DQuick Check:
ESM default export uses export default [OK]
Hint: ESM exports use export default or named export [OK]
Common Mistakes:
- Using module.exports in ESM files
- Trying to use export = which is invalid in ESM
- Confusing exports.function with ESM named exports
3. Given this CommonJS code, what will be the output?
const message = require('./message');
console.log(message);
And the file message.js contains:
module.exports = 'Hello from CommonJS';
medium
Solution
Step 1: Understand module.exports in CommonJS
The file exports a string 'Hello from CommonJS' using module.exports.Step 2: Import and log the exported value
The require call imports the string, so console.log prints it.Final Answer:
'Hello from CommonJS' -> Option AQuick Check:
CommonJS require returns module.exports value [OK]
Hint: require() returns module.exports value [OK]
Common Mistakes:
- Expecting require to return an object by default
- Confusing with ESM import behavior
- Thinking require throws error without .js extension
4. What is wrong with this ESM import statement in a Node.js file?
const fs = require('fs');medium
Solution
Step 1: Identify module system used
The code usesrequirewhich is CommonJS syntax, but in ESM modules, require is not available.Step 2: Understand ESM import rules
ESM modules must useimportstatements; require is undefined.Final Answer:
require is not defined in ESM modules -> Option BQuick Check:
require() undefined in ESM [OK]
Hint: require() is undefined in ESM modules [OK]
Common Mistakes:
- Thinking require works in ESM without extra setup
- Confusing missing semicolon as error
- Believing fs cannot be imported in Node.js
5. You want to write a Node.js module that can be imported both by CommonJS and ESM users without errors. Which approach is best?
hard
Solution
Step 1: Understand compatibility challenges
CommonJS and ESM have different import/export systems, so a single file often can't serve both seamlessly.Step 2: Use Node.js dual package support
Node.js supports conditional exports in package.json to provide separate entry points for CommonJS and ESM consumers.Step 3: Evaluate other options
Usemodule.exportsand add a separateexport defaultfor ESM mixes syntaxes which doesn't work reliably, C requires dynamic import in consumers, D limits to ESM-only.Final Answer:
Use a dual package approach with conditional exports in package.json -> Option CQuick Check:
Dual package conditional exports solve CommonJS/ESM compatibility [OK]
Hint: Use package.json conditional exports for dual support [OK]
Common Mistakes:
- Trying to mix module.exports and export default in one file
- Using only .mjs files limits CommonJS users
- Relying on dynamic import() without package config
