Guesses (MIME) Content-Type based on file name.

npm install guess-content-type
17 downloads in the last month

guess-content-type Build Status Dependency Status

Guesses (MIME) Content-Type (including charset) based on file name.

This is based on mime module, but has a more convenient interface if your goal is to explictly state that your content in text-based formats has UTF-8 encoding (which many editors have as output nowadays) . Without specifying a charset explicitly, most browsers will default to ISO-8859-1, or whatever the user preference is.

Don't use this module if you're not sure about the character encoding of the HTML or text content you're generating, because you'd be making things worse by specifying the wrong encoding. Check your editor's documentation first.

Note that problems with encoding (at least in Western countries) will only show up when you use non-ascii characters.


var guessType = require("guess-content-type")
var contentType = guessType(filename)

contentType is now the (guessed) media type for filename, or the media type including charset=UTF-8 parameter if it's a text type. This can be put in the HTTP Content-Type header.

For convenience, filename may be an entire path or url as well.

Example guesses, used as tests

guesses =
  "a.txt":                        "text/plain; charset=UTF-8"
  "b.html":                       "text/html; charset=UTF-8"
  ".jpg":                         "image/jpeg"
  ".unknown-type":                "application/octet-stream"
  "/":                            "application/octet-stream"
  "/some/path":                   "application/octet-stream"
  "":   "text/html; charset=UTF-8"
  "": "text/html; charset=UTF-8"
  "":  "image/gif"


The initial structure of this module was generated by Jumpstart, using the Jumpstart Black Coffee template.


guess-content-type is released under the MIT License.
Copyright (c) 2013 Meryn Stol

npm loves you