Vad är filformatet MJS?
En .mjs-fil är JavaScript-källkod som Node.js laddar som en ECMAScript-modul (ESM). Filändelsen är en signal: alla filer som slutar på .mjs behandlas som en ES-modul och använder import- och export-satser, även när projektets package.json inte anger någon modultyp.
Node.js lade till filändelsen i version 8.5.0 (2017), först bakom flaggan --experimental-modules, och gjorde den stabil i version 12. Den finns för att hålla isär två modulsystem. Äldre Node-kod använder CommonJS med require() och module.exports, medan modern kod använder ESM. En vanlig .js-fil är CommonJS som standard, så .mjs ger ett entydigt sätt att välja moduler. Den matchande filändelsen .cjs tvingar fram CommonJS i motsatt riktning.
Kod i en .mjs-fil körs automatiskt i „strict mode” och kan använda await på toppnivå samt objektet import.meta, vilket ingetdera fungerar i CommonJS. Paketerare som webpack, Rollup och Vite både läser och skriver .mjs.
Säkerhet & trygghet
RISK: 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.
Formatdetaljer
i ett nötskal- 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.
Program som öppnar MJS-filer
Tekniska detaljer
djup specifikation| 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 |
| Släppt | 2017 (Node.js v8.5.0) |
| Senaste version | ECMAScript 2015 module system (stabilized in Node.js v12+) |
| Öppen standard | Ja · royaltyfri |
| Specifikation | nodejs.org |
MJS-konverteringar
Frågor & svar
frågat av användareInga frågor än - bli den första att fråga om MJS-filer.