Was ist das MJS-Dateiformat?
Eine .mjs-Datei ist JavaScript-Quellcode, den Node.js als ECMAScript-Modul (ESM) lädt. Die Dateiendung ist ein Signal: Jede Datei, die auf .mjs endet, wird als ES-Modul behandelt und verwendet import- und export-Anweisungen, selbst wenn die package.json des Projekts nichts über den Modultyp aussagt.
Node.js hat die Dateiendung in Version 8.5.0 (2017) hinzugefügt, zunächst hinter dem Flag --experimental-modules, und sie in Version 12 stabilisiert. Sie existiert, um zwei Modulsysteme voneinander zu trennen. Älterer Node-Code verwendet CommonJS mit require() und module.exports, während moderner Code ESM nutzt. Eine einfache .js-Datei ist standardmäßig CommonJS, daher bietet .mjs einen eindeutigen Weg, sich für Module zu entscheiden. Die entsprechende Dateiendung .cjs erzwingt CommonJS in die andere Richtung.
Code in einer .mjs-Datei wird automatisch im „Strict Mode“ ausgeführt und kann Top-Level-await sowie das import.meta-Objekt verwenden, was beides in CommonJS nicht funktioniert. Bundler wie webpack, Rollup und Vite können .mjs sowohl lesen als auch schreiben.
Sicherheit
RISIKO: MEDIUMAn .mjs file is executable JavaScript. Running one with node executes whatever code it contains, so an untrusted .mjs file can perform harmful actions such as reading files or making network requests. Opening it in a text editor is safe; only run .mjs files from sources you trust, and review the code first.
Formatdetails
kurz gefasst- Generic minified JavaScript module - Some build tools emit .mjs to mark a bundled or minified ES module output rather than a hand-written source file.
Programme zum Öffnen von MJS-Dateien
Technische Details
technische Spezifikation| Encoding | UTF-8 plain text |
| Byte order | Not applicable (text file, no byte-order dependence; BOM optional but discouraged) |
| Container | None; raw JavaScript source |
| Typical size | A few hundred bytes to tens of kilobytes per module |
| Structure | Human-readable JavaScript source using the ECMAScript module system. Uses static import and export statements, supports top-level await and import.meta, and runs in strict mode by default. Each .mjs file is a module with its own top-level scope; variables are not shared globally. |
| Integrity | None built in; integrity is handled externally (npm package hashes, Subresource Integrity on the web, git). |
| Platforms | Windows, macOS, Linux, BSD, any OS running Node.js or a modern browser |
| Notes | Node.js treats any .mjs file as an ES module regardless of the package.json "type" field, which is the main reason the extension exists. The counterpart .cjs forces CommonJS. Browsers can also load .mjs via <script type="module"> provided the server sends a JavaScript MIME type. Bundlers such as webpack, Rollup, esbuild and Vite read and emit .mjs. |
| Runs In | Node.js runtime and modern web browsers (via script type=module) |
| Strict Mode | Always enabled; ES modules run in strict mode automatically |
| Veröffentlicht | 2017 (Node.js v8.5.0) |
| Neueste Version | ECMAScript 2015 module system (stabilized in Node.js v12+) |
| Offener Standard | Ja · lizenzgebührenfrei |
| Spezifikation | nodejs.org |
MJS-Konvertierungen
Community Q&A
von Nutzern gefragtNoch keine Fragen - stellen Sie die erste Frage zu MJS-Dateien.