What is .mjs?

.mjs files contain JavaScript code written using the ECMAScript Module (ESM) standard. This extension is used to explicitly tell Node.js and other tools to treat the file as a module, allowing the use of 'import' and 'export' statements instead of CommonJS 'require'.

This quick guide explains when to use .mjs files, how to open them on any device, and how to share them instantly with FileXhost.

When to use .mjs files

  • You are writing modern JavaScript for Node.js and want to use 'import/export' syntax natively.
  • You want to ensure your code is treated as a module regardless of the 'package.json' configuration.
  • You are building a library that needs to support both CommonJS (CJS) and ES Modules (ESM).
  • You are working with tools that rely on the .mjs extension to distinguish module types.

How to open .mjs files

MJS files are just JavaScript text files. You can open and edit them in any code editor like VS Code, Sublime Text, or Atom.

Algorithm details

ES Modules are statically analyzed, meaning imports and exports are determined before the code runs. This allows for optimizations like tree-shaking (removing unused code). In Node.js, .mjs files are always parsed as ES Modules, whereas .js files are parsed as CommonJS by default unless 'type': 'module' is set in package.json.

Browser & platform support

  • Direct: Supported by all modern browsers via <script type='module' src='file.mjs'></script>.
  • Note: Servers must serve .mjs files with the correct MIME type (text/javascript) for browsers to accept them.

Format comparison

FeatureDetails
SyntaxUses 'import x from y' and 'export const x', whereas CommonJS uses 'require(y)' and 'module.exports'.
LoadingES Modules are asynchronous, while CommonJS is synchronous.
ScopeTop-level 'this' is undefined in ESM, but refers to the module in CommonJS. ESM enables 'strict mode' by default.

How to create mjs files

  • Text Editors: Save file as 'script.mjs'.
  • Node.js: Rename any .js file to .mjs to force ESM mode.

How to convert mjs files

  • Bundlers: Tools like Webpack, Rollup, or Esbuild can bundle .mjs files into a single .js file for older browsers.
  • FileXhost: Upload to share source code.

Advantages & disadvantages

Advantages

  • Standardized module system for JavaScript (works in browser and server)
  • Allows for tree-shaking (smaller bundle sizes)
  • Clearer syntax for dependencies
  • Avoids ambiguity in Node.js projects

Disadvantages

  • Requires configuration (MIME types) on some older web servers
  • Cannot use 'require()' inside an .mjs file (must use 'import')
  • __dirname and __filename are not available (must use 'import.meta.url')

Tools & software

Runtime

Node.js (12+), Deno, Bun

Browsers

Chrome, Firefox, Safari, Edge

Bundlers

Vite, Webpack, Rollup

Frequently asked questions

Can I rename .js to .mjs?

Yes, but if your code uses 'require()' or 'module.exports', it will break. You must rewrite those to use 'import' and 'export'.

Do browsers support .mjs?

Yes, but only if you include the script with `type='module'`. Also, the server must send the `text/javascript` MIME type.

Why not just use .js?

In Node.js, .js is treated as CommonJS by default. Using .mjs forces Node to treat it as an ES Module, which is useful if you don't want to change the entire project's package.json.

Technical specs

File type
Code
Extension
.mjs
MIME type
text/javascript, application/javascript
Compression
Uncompressed
Max file size on FileXhost
Up to 25 MB per file on the free plan and up to 1 GB on Pro FileXhost accounts.

Share .mjs files instantly

Upload your .mjs file to FileXhost to get a clean, shareable URL in seconds. View the file in a modern browser, protect access with optional settings, and let others download it without any confusing ads or cluttered file pages.

Upload .mjs file