(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:
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.
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:
text/javascript
is the most commonly used type:
.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.
On Node.js we have two options:
.mjs
without configuration..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
.
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
).
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
.