What is the difference between .js, .cjs and .mjs file extension
CJS, MJS, and .JS are file extensions used to denote different types of JavaScript files. Here's the difference between them:
.JS (JavaScript):
The .js extension is the most common file extension for JavaScript files. It is used to indicate that a file contains JavaScript code. .js files can be executed in different JavaScript environments, such as web browsers, servers, and other JavaScript runtime environments.CJS (CommonJS):
CommonJS is a module system for JavaScript used in server-side environments like Node.js. The CJS module format allows you to define modules using therequire
andmodule.exports
syntax. In CommonJS, each file is treated as a separate module, and you can import/export functionality between modules usingrequire
andmodule.exports
.
CommonJS modules are typically used in Node.js applications and other server-side JavaScript environments. You will often find files with the .js extension using the CommonJS module format.
- MJS (ECMAScript Modules): MJS is an extension used for JavaScript files that adhere to the ECMAScript Modules (ESM) specification. ECMAScript modules are part of the JavaScript language standard and provide a more modern and standardized way to define modules.
ECMAScript modules use the import
and export
keywords to define dependencies and expose functionality between modules. Unlike CommonJS, which is primarily used in server-side environments, ECMAScript modules can be used both in browsers and server-side environments that support them.
The MJS extension is often used to indicate that a JavaScript file uses the ECMAScript module format, making it easier to differentiate between files that use CommonJS and ECMAScript modules.
In summary, .js files are the general extension for JavaScript files, while CJS and MJS are extensions used to specify the module formats (CommonJS and ECMAScript Modules) used in JavaScript files for different environments and purposes.
Here are some examples to illustrate the usage of each file extension:
- .JS (JavaScript):
// script.js
function sayHello() {
console.log("Hello, world!");
}
sayHello();
- CJS (CommonJS):
// moduleA.js
const message = "Hello, from Module A!";
module.exports = message;
// moduleB.js
const messageA = require("./moduleA");
console.log(messageA);
- MJS (ECMAScript Modules):
// moduleA.mjs
const message = "Hello, from Module A!";
export default message;
// moduleB.mjs
import messageA from "./moduleA";
console.log(messageA);
In the first example, the .js file contains regular JavaScript code that can be executed in any JavaScript environment.
The second example demonstrates the use of CommonJS modules. Each file is treated as a separate module, and the module.exports statement is used to expose functionality from one module to another. The require statement is used to import the exported functionality.
The third example showcases ECMAScript modules. The MJS extension is used to indicate that the JavaScript file adheres to the ECMAScript module format. The export statement is used to expose functionality, and the import statement is used to import the exported functionality from other modules.
Subscribe to my newsletter
Read articles from Parveen Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Parveen Kumar
Parveen Kumar
I am a Full-stack MERN engineer with a product centric approach, specializing in the design and development of scalable, reliable, production ready web application, I bring expertise in JavaScript, React, and Node Js. Get in touch! paulparveen01@gmail.com