Discover why choosing the right module system can save you hours of debugging and confusion!
CommonJS vs ESM differences in Node.js - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have many JavaScript files in your Node.js project, and you need to share code between them. You try to manually copy and paste functions everywhere or write complex scripts to load files in the right order.
Manually managing code sharing is confusing and error-prone. You might load files in the wrong order, cause naming conflicts, or struggle to reuse code efficiently. It slows down development and makes your project hard to maintain.
CommonJS and ESM are two systems that let you easily import and export code between files. They handle loading and sharing automatically, so you can focus on writing your app instead of managing file dependencies.
const utils = require('./utils');
const data = utils.loadData();import { loadData } from './utils.js'; const data = loadData();
These module systems let you build clean, organized, and reusable code that works smoothly across your Node.js projects.
Think of a recipe book where each recipe is a module. CommonJS and ESM help you pick and use recipes easily without rewriting them every time you cook.
Manual code sharing is slow and error-prone.
CommonJS and ESM automate code importing and exporting.
They help keep projects organized and maintainable.
Practice
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]
- Confusing ESM import syntax with CommonJS
- Using import in CommonJS files
- Trying to use module.import() which doesn't exist
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]
- Using module.exports in ESM files
- Trying to use export = which is invalid in ESM
- Confusing exports.function with ESM named exports
const message = require('./message');
console.log(message);
And the file message.js contains:
module.exports = 'Hello from CommonJS';
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]
- Expecting require to return an object by default
- Confusing with ESM import behavior
- Thinking require throws error without .js extension
const fs = require('fs');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]
- Thinking require works in ESM without extra setup
- Confusing missing semicolon as error
- Believing fs cannot be imported in Node.js
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]
- 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
