What Is .cjs

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 10, 2026

Quick Answer: .cjs stands for CommonJS and is a file extension that explicitly marks Node.js modules as using the CommonJS module system, allowing require() and module.exports syntax in projects configured for ES modules. Introduced as a standard practice around 2018-2019, it enables developers to maintain dual-module packages serving both CommonJS and ES module consumers from a single codebase.

Key Facts

Overview

.cjs stands for CommonJS and is a file extension that explicitly designates Node.js modules as CommonJS format, regardless of package.json configuration. While CommonJS has been the default module system since Node.js was created in 2009, the introduction of native ES module support created ambiguity about which module system a .js file should use. The .cjs extension emerged as the solution, providing explicit clarity and enabling coexistence of both systems.

As of 2024, the JavaScript ecosystem is transitioning from CommonJS to ES modules, but this transition is gradual because millions of existing packages rely on CommonJS. The .cjs extension is critical infrastructure for this transition period, allowing package maintainers to distribute dual-format libraries and enabling teams to gradually migrate mixed codebases. Understanding .cjs is essential for package authors, Node.js developers, and anyone working with modern JavaScript infrastructure.

How It Works

The .cjs extension operates through Node.js module resolution, which automatically treats files with this extension as CommonJS regardless of how package.json is configured:

Key Comparisons

Aspect.cjs (CommonJS).mjs (ES Module).js (Context-Dependent)
File Extension.cjs.mjs.js
Module Syntaxrequire() and module.exportsimport and export statementsDetermined by "type" field in package.json
Node.js SupportAll versions since v0.1.0v12.20.0+ with stable supportAll versions (behavior depends on config)
Static AnalysisLimited, requires runtime analysisFull static analysis possibleDepends on determined module system
Primary Use CaseLegacy code, server packages, backward compatibilityModern projects, browser-friendly codeGeneral purpose files (config determines meaning)
Distribution StrategyUsed with .mjs for dual-module packagesUsed with .cjs for dual-module packagesSingle format (requires clear configuration)

Why It Matters

Today, .cjs represents a pragmatic bridge between Node.js's past and future. While ES modules are now the recommended standard for new projects, .cjs ensures that the massive existing codebase remains functional and maintainable. Mastering .cjs is essential for anyone publishing packages, working with modern Node.js applications, or building tools in the JavaScript ecosystem, as it remains the dominant module format across npm for the foreseeable future.

Sources

  1. Node.js Official Documentation - PackagesCC-BY-4.0
  2. MDN Web Docs - JavaScript ModulesCC-BY-SA-2.5
  3. Node.js Official Guide - Building ApplicationsCC-BY-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.