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
Also in Daily Life
More "What Is" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Node.js ECMAScript Modules DocumentationCC-BY-SA-4.0
- MDN Web Docs - JavaScript ModulesCC-BY-SA-4.0
- Node.js CommonJS DocumentationCC-BY-SA-4.0