Co to jest format pliku MJS?
Plik .mjs to kod źródłowy JavaScript, który Node.js ładuje jako moduł ECMAScript (ESM). Rozszerzenie to jest sygnałem: każdy plik kończący się na .mjs jest traktowany jako moduł ES, używający instrukcji import i export, nawet jeśli plik package.json projektu nie zawiera informacji o typie modułu.
Node.js dodał to rozszerzenie w wersji 8.5.0 (2017), początkowo pod flagą --experimental-modules, a ustabilizował je w wersji 12. Istnieje ono, aby oddzielić dwa systemy modułów. Starszy kod Node używa CommonJS z require() i module.exports, podczas gdy nowoczesny kod korzysta z ESM. Zwykły plik .js jest domyślnie traktowany jako CommonJS, więc .mjs daje jednoznaczny sposób na przejście na moduły. Pasujące rozszerzenie .cjs wymusza CommonJS w drugą stronę.
Kod w pliku .mjs jest automatycznie uruchamiany w trybie ścisłym (strict mode) i może używać await na najwyższym poziomie oraz obiektu import.meta, z których żaden nie działa w CommonJS. Narzędzia do budowania, takie jak webpack, Rollup i Vite, potrafią zarówno odczytywać, jak i zapisywać pliki .mjs.
Bezpieczeństwo
RYZYKO: 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.
Szczegóły formatu
w pigułce- 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.
Programy otwierające pliki MJS
Szczegóły techniczne
specyfikacja| 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 |
| Wydano | 2017 (Node.js v8.5.0) |
| Najnowsza wersja | ECMAScript 2015 module system (stabilized in Node.js v12+) |
| Otwarty standard | Tak · bezpłatny (royalty-free) |
| Specyfikacja | nodejs.org |
Konwersje MJS
Pytania i odpowiedzi społeczności
pytania użytkownikówBrak pytań - bądź pierwszą osobą, która zapyta o pliki MJS.