About JavaScript module files
The .mjs extension marks a file as an ES module in Node.js, but on the web it is served exactly like .js. Older server configs don’t list .mjs and send application/octet-stream, which browsers reject for module scripts. Map it to text/javascript if your server doesn’t.
Other types seen for these files:
application/javascript
. Accept them when reading uploads, but send text/javascript yourself.
File signature
MJS has no fixed signature: it is text without a magic number, so validate uploads by parsing the content rather than by checking bytes.
Serve .mjs with the right Content-Type
Content-Type: text/javascript; charset=utf-8
text/javascript mjs;
AddType text/javascript .mjs
AddCharset UTF-8 .mjs
@mjs path *.mjs
header @mjs Content-Type "text/javascript; charset=utf-8"
curl -sI https://example.com/file.mjs | grep -i '^content-type'
# Content-Type: text/javascript; charset=utf-8
Set the type in code
res.set('Content-Type', 'text/javascript; charset=utf-8');
import mimetypes
mimetypes.add_type('text/javascript', '.mjs') # if your system doesn't know it
mimetypes.guess_type('file.mjs')[0] # 'text/javascript'
mime.AddExtensionType(".mjs", "text/javascript; charset=utf-8")
w.Header().Set("Content-Type", "text/javascript; charset=utf-8")
Accept .mjs uploads
<input type="file" accept=".mjs,text/javascript">
The accept attribute only filters the file picker; users can still choose another file. Listing the
extension as well as the type matters, because operating systems don’t always report the type you expect.
Related types
-
.js (
text/javascript): JavaScript -
.wasm (
application/wasm): WebAssembly module -
.json (
application/json): JSON
More web
.html , .css , .svg , .webmanifest , .rss , .vtt . Look up any extension or type with the MIME type lookup.
Sources
Spotted a mistake? Email hello@devtoolsmith.com and we’ll fix it.
Frequently asked questions
What is the MIME type for .mjs files?
text/javascript. You may also see application/javascript, which is older or unofficial; send text/javascript. It is a text format, so add the character set: "Content-Type: text/javascript; charset=utf-8".
Why does my server send the wrong Content-Type for .mjs?
Web servers pick the type from the file extension using a table (mime.types in nginx and Apache). If .mjs is missing from that table, they send a default such as application/octet-stream or text/plain. Add the mapping as shown on this page, then check the response with "curl -sI".
How can I check that a file really is MJS?
MJS has no fixed file signature, so the extension and content are all you have. Check that the content is what you expect, for example by parsing it, before trusting an upload.