What is mjs file

Last updated: April 1, 2026

Quick Answer: An MJS file is a JavaScript file with the .mjs extension that explicitly uses ECMAScript module syntax, allowing modern import and export statements instead of CommonJS require() statements.

Key Facts

What is an MJS File?

An MJS file is a JavaScript file with the .mjs (Module JavaScript) extension that exclusively uses ECMAScript module syntax. The .mjs extension explicitly signals to JavaScript runtimes like Node.js that the file contains ES module code using import and export statements. This distinction from traditional .js files (which may use CommonJS syntax) provides clarity about how the file should be processed and enables developers to use modern JavaScript module features effectively.

MJS File Syntax

MJS files use ECMAScript module syntax exclusively, which includes import statements to load dependencies and export statements to share functionality with other modules. For example, an MJS file might begin with import statements like "import express from 'express';" and conclude with export statements like "export default app;" MJS files cannot use CommonJS require() or module.exports syntax; they must use the modern import/export pattern throughout the entire file.

How Node.js Handles MJS Files

When Node.js encounters a .mjs file, it automatically treats it as an ES module, loading it asynchronously and enabling all ES module features. This eliminates the need to configure package.json with "type": "module" just for individual files. The .mjs extension provides explicit module type declaration at the file level. This behavior makes .mjs files particularly useful in projects that mix module types or provide library distributions in multiple formats for different use cases.

MJS Files vs .JS Files

The distinction between MJS and traditional .JS files is significant for module handling:

Use Cases for MJS Files

MJS files are ideal for several specific scenarios in JavaScript development. Library authors use .mjs files to provide ES module distributions of their packages while maintaining CommonJS versions. Modern web frameworks and tools default to .mjs for their source files. Developers building projects that exclusively use ES modules benefit from .mjs extension clarity. Projects that gradually migrate from CommonJS to ES modules can use .mjs files during the transition period.

Best Practices for MJS Files

When working with MJS files, maintain consistency by using import/export statements throughout the entire file structure. Clearly document your project's module strategy, especially if mixing CommonJS and ES modules. Ensure your build tools and dependencies support ES modules if you're using MJS extensively. Modern development tools generally handle MJS well, but always verify compatibility with your specific project requirements and dependencies.

Related Questions

What is the difference between .mjs and .cjs files?

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

Can I use .mjs files in a Node.js project with package.json?

Yes, you can use .mjs files in any Node.js project. The .mjs extension explicitly indicates ES module syntax, so Node.js treats it as an ES module even if package.json specifies "type": "commonjs". This makes .mjs useful for gradually migrating to ES modules.

What Node.js versions support .mjs files?

Modern versions of Node.js support .mjs files well, with support generally available in Node.js 12.x and later versions. Older versions may have limited or no support for ES modules. It is recommended to use Node.js 14.x or later for reliable .mjs file support and features.

Sources

  1. Node.js Documentation - ECMAScript Modules MIT
  2. MDN - JavaScript Modules Guide CC-BY-SA-2.5