DevToolsmith

JS MIME Type

text/javascript is the MIME type for JavaScript files. Here is the Content-Type header to send, the file signature, and how to configure servers and code.

MIME type
text/javascript
Extensions
.js, .cjs
Content-Type header
text/javascript; charset=utf-8
Registered
Yes, with IANA
Compress with gzip
Yes, it is text or uncompressed
Category
Web

About JavaScript files

RFC 9239 (2022) made text/javascript the official type and marked application/javascript and application/x-javascript as obsolete; browsers accept all of them. Module scripts (<script type="module"> and import) are strict: if the server answers with any non-JavaScript type, the import fails.

  • “Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of text/html” means the request got an HTML page, typically a 404 or an SPA rewrite that sends index.html for every path.

Other types seen for these files: application/javascript , application/x-javascript . Accept them when reading uploads, but send text/javascript yourself.

File signature

JS has no fixed signature: it is text without a magic number, so validate uploads by parsing the content rather than by checking bytes.

Serve .js with the right Content-Type

Response header
Content-Type: text/javascript; charset=utf-8
nginx (a line in the types { } block of mime.types)
text/javascript js cjs;
Apache (.htaccess or server config)
AddType text/javascript .js .cjs
AddCharset UTF-8 .js .cjs
Caddy
@js path *.js *.cjs
header @js Content-Type "text/javascript; charset=utf-8"
Check what a server sends
curl -sI https://example.com/file.js | grep -i '^content-type'
# Content-Type: text/javascript; charset=utf-8

Set the type in code

Node.js (Express)
res.set('Content-Type', 'text/javascript; charset=utf-8');
Python
import mimetypes

mimetypes.add_type('text/javascript', '.js')  # if your system doesn't know it
mimetypes.guess_type('file.js')[0]  # 'text/javascript'
Go
mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
w.Header().Set("Content-Type", "text/javascript; charset=utf-8")

Accept .js uploads

HTML
<input type="file" accept=".js,.cjs,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

  • .mjs (text/javascript): JavaScript module
  • .css (text/css): CSS stylesheet
  • .wasm (application/wasm): WebAssembly module
  • .ts (video/mp2t): MPEG transport stream

More web

.html , .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 .js files?

text/javascript. You may also see application/javascript, application/x-javascript, which are 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 .js?

Web servers pick the type from the file extension using a table (mime.types in nginx and Apache). If .js 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 JS?

JS 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.