CommonJS is a project started in 2009 by Kevin Dangoor to expand JavaScript use. CommonJS modules allow JavaScript code sharing between files and environments like Node.js. CJS files contain JavaScript code in CommonJS syntax. CJS files can be opened and edited in any text editor.
Node.js supports CommonJS modules and ECMAScript modules. By default, Node.js treats files as CommonJS if they have .cjs extension or .js extension with “type”:”commonjs” in nearest parent package.json.
JavaScript files in Node.js projects often use .js extension. You may also see .mjs for ECMAScript modules or .cjs for CommonJS. The extensions indicate module formats.
.js is general JavaScript. CJS is CommonJS modules. MJS is ECMAScript modules. For example:
// script.js
Function to log message
// moduleA.cjs
Exports message variable
The order was changed to group information on CommonJS together, then explain differences between file types, and finally give examples. Unnecessary details and repetitions were removed. Sentence length ranges from 6 to 16 words to improve flow.