RFC 9239: Updates to ECMAScript media types

[2022-05-18] dev, javascript
(Ad, please don’t block)

(This blog post is based on a tweet thread and additional input by Mathias Bynens.)

After work started on it in August 2017, May 2022 finally saw the publication of RFC 9239 “Updates to ECMAScript media types” by Matthew A. Miller, Myles Borins, Mathias Bynens, and Bradley Farias. It updates JavaScript MIME type registrations to align with reality:

  • The JavaScript MIME type is now unambiguously text/javascript.
  • .mjs is now a registered filename extension, specifically for JavaScript modules.

This unblocks tooling and server software like Apache to support JavaScript modules out of the box in a unified manner, like e.g. Node.js already does. Better industry alignment on MIME type and file extensions increases interoperability across tooling and other software.

What are the consequences of text/javascript now being the only standard MIME type for JavaScript?  

text/javascript is now the only standard MIME type for JavaScript. All others are considered historical and obsolete aliases.

That doesn’t change anything in practice because web browsers still have to accept all JavaScript MIME types. But it resolves a long-standing disagreement between standards:

Do we have to use the filename extension .mjs for modules now?  

The RFC does not mean that we have to use the filename extension .mjs for modules – we can continue to use whatever extension we like. In other words, this RFC is good news for everyone – regardless of personal preferences w.r.t. filename extensions.

Let’s examine how filename extensions work on various JavaScript platforms.

Module filename extensions on Node.js  

On Node.js we have two options:

  • Use .mjs without configuration.
  • Use .js and add "type": "module" to package.json.

We can also pass “string input” to Node.js via --eval, --print, or standard input. To interpret such input as an ECMAScript module, use --input-type=module.

Module filename extensions on Deno  

Deno supports the filename extensions .ts, .js, and .mjs. Files with these extensions are always interpreted as ECMAScript modules (in TypeScript syntax if the extension is .ts).

Module filename extensions on web browsers  

Web browsers don’t care about filename extensions at all, only about MIME types. Therefore, we can use any filename extension as long as our web server serves the files as text/javascript.

Further reading