What is cjs and esm

Last updated: April 1, 2026

Quick Answer: CJS (CommonJS) and ESM (ECMAScript Modules) are two different module systems in JavaScript. CJS is the older Node.js standard using require() and module.exports, while ESM is the modern JavaScript standard using import and export statements.

Key Facts

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.

FeatureCommonJS (CJS)ESM
Import Syntaxrequire('module')import from 'module'
Export Syntaxmodule.exports = {}export default {} / export {}
Loading TypeSynchronousAsynchronous
Tree-shakingLimitedFull support
Top-level awaitNot supportedSupported
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.

Sources

  1. Node.js ECMAScript Modules Documentation CC-BY-SA-4.0
  2. MDN Web Docs - JavaScript Modules CC-BY-SA-4.0
  3. Node.js CommonJS Documentation CC-BY-SA-4.0