O que é o formato de ficheiro MJS?
Um arquivo .mjs é um código-fonte JavaScript que o Node.js carrega como um módulo ECMAScript (ESM). A extensão é um sinal: qualquer arquivo que termine em .mjs é tratado como um módulo ES, usando declarações import e export, mesmo quando o package.json do projeto não especifica nada sobre o tipo de módulo.
O Node.js adicionou a extensão na versão 8.5.0 (2017), primeiro sob a flag --experimental-modules, e a tornou estável na versão 12. Ela existe para manter dois sistemas de módulos separados. Códigos mais antigos do Node usam CommonJS com require() e module.exports, enquanto o código moderno usa ESM. Um arquivo .js simples é CommonJS por padrão, então o .mjs oferece uma maneira inequívoca de optar pelos módulos. A extensão correspondente .cjs força o CommonJS na direção oposta.
O código em um arquivo .mjs é executado automaticamente no modo estrito («strict mode») e pode usar await de nível superior e o objeto import.meta, nenhum dos quais funciona no CommonJS. Empacotadores como webpack, Rollup e Vite leem e gravam .mjs.
Segurança e proteção
RISCO: 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.
Detalhes do formato
em resumo- 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 abrem arquivos MJS
Detalhes técnicos
especificação 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 |
| Lançado | 2017 (Node.js v8.5.0) |
| Versão mais recente | ECMAScript 2015 module system (stabilized in Node.js v12+) |
| Padrão aberto | Sim · livre de royalties |
| Especificação | nodejs.org |
Conversões de MJS
Perguntas e Respostas da Comunidade
perguntado por usuáriosAinda não há perguntas - seja o primeiro a perguntar sobre arquivos MJS.