MJS ファイル形式とは?
An .mjs file is JavaScript source code that Node.js loads as an ECMAScript module (ESM). The extension is a signal: any file ending in .mjs is treated as an ES module, using import and export statements, even when the project's package.json says nothing about module type.
Node.js added the extension in version 8.5.0 (2017), first behind the --experimental-modules flag, and made it stable in version 12. It exists to keep two module systems apart. Older Node code uses CommonJS with require() and module.exports, while modern code uses ESM. A plain .js file is CommonJS by default, so .mjs gives an unambiguous way to opt into modules. The matching .cjs extension forces CommonJS in the other direction.
Code in an .mjs file runs in strict mode automatically and can use top-level await and the import.meta object, neither of which works in CommonJS. Bundlers such as webpack, Rollup and Vite both read and write .mjs.
セキュリティと安全性
リスク: 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.
形式の詳細
概要- 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.
MJS ファイルを開くプログラム
技術的詳細
詳細仕様| 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 |
| リリース日 | 2017 (Node.js v8.5.0) |
| 最新バージョン | ECMAScript 2015 module system (stabilized in Node.js v12+) |
| オープンスタンダード | はい · ロイヤリティフリー |
| 仕様書 | nodejs.org |
MJS の変換
コミュニティ Q&A
ユーザーからの質問まだ質問はありません。MJS ファイルについて最初の質問をしてみましょう。