DEV Community

Nipu Chakraborty
Nipu Chakraborty

Posted on

.js, .cjs and .mjs defference

CJS, MJS, and .JS are file extensions used to denote different types of JavaScript files. Here's the difference between them:

  1. .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.

  2. 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 the require and module.exports syntax. In CommonJS, each file is treated as a separate module, and you can import/export functionality between modules using require and module.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.

  1. 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:

  1. .JS (JavaScript):
// script.js
function sayHello() {
  console.log("Hello, world!");
}

sayHello();
Enter fullscreen mode Exit fullscreen mode
  1. CJS (CommonJS):
// moduleA.js
const message = "Hello, from Module A!";
module.exports = message;

// moduleB.js
const messageA = require("./moduleA");
console.log(messageA);
Enter fullscreen mode Exit fullscreen mode
  1. MJS (ECMAScript Modules):
// moduleA.mjs
const message = "Hello, from Module A!";
export default message;

// moduleB.mjs
import messageA from "./moduleA";
console.log(messageA);
Enter fullscreen mode Exit fullscreen mode

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.

Please note that the usage of CJS or MJS modules depends on the specific JavaScript runtime or environment being used.

ref

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.