What is mjs

Last updated: April 1, 2026

Quick Answer: MJS refers to ES modules, the .mjs file extension used in JavaScript environments like Node.js to denote files using modern ECMAScript module syntax with import and export statements.

Key Facts

What Does MJS Stand For?

MJS stands for ES modules (ECMAScript modules) and refers to the .mjs file extension used in JavaScript environments, particularly Node.js. The .mjs extension is a standardized way to indicate that a JavaScript file uses modern ECMAScript module syntax rather than the older CommonJS format. This distinction helps developers and tools understand how to properly process the file's dependencies and exports in JavaScript applications.

How MJS Works

MJS files use ECMAScript module syntax, which includes import statements to bring in dependencies and export statements to share code with other modules. This differs from CommonJS files (.js files) that use require() for importing and module.exports for sharing code. When Node.js encounters an .mjs file, it automatically treats it as an ES module regardless of package.json configuration, making the module type explicit and unambiguous.

MJS Advantages

Using .mjs files offers several significant advantages for JavaScript development:

MJS vs CommonJS

The primary difference between MJS and CommonJS lies in their syntax and module loading mechanisms. CommonJS uses synchronous require() and module.exports, while MJS uses asynchronous import and export statements. MJS files must use the import/export syntax exclusively, whereas CommonJS files use require(). This distinction is important when deciding how to structure JavaScript projects and manage dependencies across your codebase.

When to Use MJS

MJS files are ideal for modern JavaScript projects that want to leverage ES module features and modern language capabilities. Package authors commonly use .mjs files when providing ES module distributions of their libraries. Modern frameworks and tools increasingly default to MJS for new projects. If you're starting a new JavaScript project or modernizing an existing codebase, using .mjs files allows you to take advantage of the latest JavaScript features and development best practices.

Related Questions

What is the difference between MJS and CJS files?

MJS (.mjs) files use ES module syntax with import/export statements, while CJS (.cjs or .js) files use CommonJS syntax with require() and module.exports. MJS files are loaded asynchronously while CommonJS files are loaded synchronously, and both formats can coexist in modern projects.

Can I use MJS in the browser?

Yes, modern browsers support ES modules natively, and files with .mjs extension can be used in browsers by including them with script tags using the type="module" attribute. This enables the same modular approach in browser-based JavaScript applications.

Do I need to change my package.json to use MJS files?

No, the .mjs extension explicitly tells Node.js to treat the file as an ES module, so you don't need to modify package.json. However, if you want all .js files treated as modules, you can set "type": "module" in package.json for your entire project.

Sources

  1. Node.js Documentation - ECMAScript ModulesMIT
  2. MDN - JavaScript Modules GuideCC-BY-SA-2.5