What is cjs and esm
Last updated: April 1, 2026
Key Facts
- CommonJS (CJS) is the module system used traditionally in Node.js with require() function
- ESM (ECMAScript Modules) is the official JavaScript module standard adopted in ES6/ES2015
- CJS is synchronous and loads modules immediately, while ESM is asynchronous and allows top-level await
- ESM supports tree-shaking and static analysis better than CommonJS
- Most modern JavaScript projects are transitioning to ESM for better compatibility and performance
CommonJS (CJS)
CommonJS is a module system developed for Node.js that predates the official JavaScript module specification. It uses require() to import modules and module.exports to export functionality. CJS loads modules synchronously, meaning the entire module is loaded before code continues executing. This approach works well for server-side applications where performance of synchronous loading is less critical.
ECMAScript Modules (ESM)
ECMAScript Modules (ESM) is the official module standard introduced in ES6/ES2015. It uses import and export statements with a more intuitive syntax. ESM is asynchronous by default, allowing modules to load in parallel. This approach aligns JavaScript with web standards and enables better optimization techniques like tree-shaking, which removes unused code from bundled applications.
Key Differences
Syntax: CJS uses require() and module.exports, while ESM uses import/export statements. Loading: CJS is synchronous, ESM is asynchronous. Performance: ESM enables better tree-shaking and code optimization. Top-level await: ESM supports top-level await, CJS does not. Static analysis: ESM allows static analysis of dependencies, while CJS requires dynamic evaluation.
Migration and Compatibility
The JavaScript ecosystem is gradually transitioning from CommonJS to ESM. Modern frameworks and tools increasingly default to ESM, though Node.js still supports CommonJS for backward compatibility. Many projects use build tools like Webpack, Rollup, or esbuild to transpile code between formats. Developers working with Node.js modules and frontend bundles should understand both systems during this transition period.
| Feature | CommonJS (CJS) | ESM | |
|---|---|---|---|
| Import Syntax | require('module') | import from 'module' | |
| Export Syntax | module.exports = {} | export default {} / export {} | |
| Loading Type | Synchronous | Asynchronous | |
| Tree-shaking | Limited | Full support | |
| Top-level await | Not supported | Supported | |
| File Extension | .js | .mjs or .js with package.json type |
Related Questions
What is tree-shaking in JavaScript?
Tree-shaking is a bundling optimization that removes unused code from the final bundle, reducing file size. It works by analyzing static imports to determine which exports are actually used.
How do I use ESM in Node.js?
You can use ESM in Node.js by adding "type": "module" to package.json, using .mjs file extension, or using a transpiler. Modern Node.js versions have strong ESM support.
Can I use both CJS and ESM together?
Yes, modern Node.js and build tools support mixing CommonJS and ESM. However, CJS cannot dynamically import ESM modules, while ESM can import CJS with some limitations.
More What Is in Daily Life
- What Is a Credit ScoreA credit score is a three-digit number, typically ranging from 300 to 850, that represents your cred…
- What Is CD rates make no sense based on length of time invested. Explain like I'm 5CD (Certificate of Deposit) rates often don't increase with longer lock-up times the way people expe…
- What is a phdA PhD (Doctor of Philosophy) is a doctoral degree earned after completing advanced academic research…
- What is a polymathA polymath is a person with deep knowledge and expertise across multiple different fields or academi…
- What is aaveAAVE stands for African American Vernacular English, a dialect with distinct grammar, pronunciation,…
- What is aarch64ARMv8-A (commonly called ARM64 or AArch64) is a 64-bit processor architecture developed by ARM Holdi…
- What is about menTopics and discussions about men typically encompass masculinity, male identity, gender roles, men's…
- What is abiturAbitur is the German academic qualification awarded upon completion of secondary education, typicall…
- What is abrosexualAbrosexual is a sexual orientation identity where a person's sexual attraction changes or fluctuates…
- What is abgABG is an Indonesian acronym standing for 'Anak Baru Gede,' which refers to adolescent girls or teen…
- What is aaaAAA batteries are a standard cylindrical battery size measuring 10.5mm in diameter and 44.5mm in len…
- What is aacAAC (Advanced Audio Codec) is a digital audio compression format that provides better sound quality …
- What is aaa gameAAA games are high-budget video games developed by large studios with budgets typically exceeding $1…
- What is a proxyA proxy is a server that acts as an intermediary between your device and the internet, forwarding yo…
- What is ableismAbleism is discrimination and prejudice against people with disabilities based on the assumption tha…
- What is absAbs, short for abdominal muscles, are the muscles in your core that flex your spine and stabilize yo…
- What is abortionAbortion is a medical procedure that ends pregnancy by removing the fetus before viability. It can b…
- What is accutaneAccutane (isotretinoin) is a powerful prescription medication derived from vitamin A used to treat s…
- What is acetaminophenAcetaminophen, also known as paracetamol, is an over-the-counter pain reliever and fever reducer use…
- What is acidAcid is a chemical substance that donates protons (hydrogen ions) to other substances, characterized…
Also in Daily Life
- How To Save Money
- Why are so many white supremacist and right wings grifters not white
- Does "I'm 20 out" mean youre 20 minutes away from where you left, or youre 20 minutes away from your destination
- Why are so many men convinced that they are ugly
- What does awol mean
- What does asl mean
- What does ad mean
- What does asap mean
- What does apex mean
- What does asmr stand for
- What does atp mean
- What causes autism
- What does abg mean
- What does am and pm mean
- What does a fox sound like
More "What Is" Questions
Trending on WhatAnswer
Browse by Topic
Browse by Question Type
Sources
- Node.js ECMAScript Modules Documentation CC-BY-SA-4.0
- MDN Web Docs - JavaScript Modules CC-BY-SA-4.0
- Node.js CommonJS Documentation CC-BY-SA-4.0