¿Qué es el formato de archivo MJS?
Un archivo .mjs es código fuente de JavaScript que Node.js carga como un módulo ECMAScript (ESM). La extensión es una señal: cualquier archivo que termine en .mjs se trata como un módulo ES, utilizando sentencias import y export, incluso cuando el package.json del proyecto no especifica nada sobre el tipo de módulo.
Node.js añadió la extensión en la versión 8.5.0 (2017), primero bajo el flag --experimental-modules, y la hizo estable en la versión 12. Existe para mantener separados dos sistemas de módulos. El código antiguo de Node utiliza CommonJS con require() y module.exports, mientras que el código moderno utiliza ESM. Un archivo .js normal es CommonJS por defecto, por lo que .mjs ofrece una forma inequívoca de optar por los módulos. La extensión correspondiente .cjs fuerza el uso de CommonJS en la dirección opuesta.
El código en un archivo .mjs se ejecuta automáticamente en modo estricto («strict mode») y puede usar await de nivel superior y el objeto import.meta, ninguno de los cuales funciona en CommonJS. Empaquetadores como webpack, Rollup y Vite leen y escriben .mjs.
Seguridad y protección
RIESGO: 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.
Detalles del formato
en pocas palabras- 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.
Programas que abren archivos MJS
Detalles técnicos
especificación profunda| 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 |
| Lanzado | 2017 (Node.js v8.5.0) |
| Última versión | ECMAScript 2015 module system (stabilized in Node.js v12+) |
| Estándar abierto | Sí · libre de regalías |
| Especificación | nodejs.org |
Conversiones de MJS
Preguntas y respuestas de la comunidad
preguntado por usuariosAún no hay preguntas; sea el primero en preguntar sobre los archivos MJS.