Learn how to get the MIME type of files in Node.js using the mime-types
, path
, and file-type
packages. Includes code examples for static and dynamic detection.

π How to Detect File MIME Type in Node.js: Reliable Methods & Examples
When building file upload services, APIs, or static file servers in Node.js, it’s important to accurately determine a file’s MIME type. The MIME type tells browsers or APIs what kind of content to expect β like "text/html"
for HTML files or "image/png"
for PNG images.
In this blog, youβll learn:
- What a MIME type is
- How to detect MIME types using file extensions
- How to detect MIME types using file content (buffer)
- Best libraries for MIME detection in Node.js
π What Is a MIME Type?
MIME (Multipurpose Internet Mail Extensions) types describe the nature and format of a file. For example:
File Extension | MIME Type |
---|---|
.html | text/html |
.png | image/png |
.pdf | application/pdf |
π Method 1: Detect MIME Type by File Extension (using mime-types
)
Install the mime-types
package:
npm install mime-types
Usage:
const mime = require('mime-types');
const filePath = 'example.pdf';
const mimeType = mime.lookup(filePath);
console.log(`MIME type: ${mimeType}`); // application/pdf
β Best for static servers and known file types
β οΈ Not secure for validating untrusted uploads
π Method 2: Detect MIME Type from Buffer (using file-type
)
If you need to inspect the actual file content, use file-type
.
Install:
npm install file-type
Usage:
const fs = require('fs');
const { fileTypeFromFile } = require('file-type');
async function detectMimeType(filePath) {
const result = await fileTypeFromFile(filePath);
console.log(result); // { ext: 'png', mime: 'image/png' }
}
detectMimeType('image.png');
β Secure and accurate
β Works even if file extension is wrong
β οΈ Slightly slower due to content inspection
π mime-types
vs file-type
Feature | mime-types | file-type |
---|---|---|
Based on file extension | β Yes | β No |
Based on file content | β No | β Yes |
Ideal for trusted files | β Yes | β Yes |
Ideal for untrusted input | β No | β Yes |
π Bonus: Combine Both for Safety
A hybrid approach for safer validation:
const mime = require('mime-types');
const { fileTypeFromFile } = require('file-type');
async function safeMimeCheck(filePath) {
const extMime = mime.lookup(filePath);
const contentMime = await fileTypeFromFile(filePath);
console.log('From extension:', extMime);
console.log('From content:', contentMime?.mime);
}
π Use Cases
- File upload validation in APIs
- Building a file previewer
- Preventing malicious uploads (e.g., disguised
.exe
) - Serving correct headers in Express static file servers
π¦ Related Libraries
mime-types
β Lookup by extensionfile-type
β Detect from bufferpath
β Extract extensions
π§ͺ Conclusion
Detecting MIME types in Node.js is easy with the right tools. Use:
mime-types
for fast lookups from file extensions.file-type
for reliable MIME detection from actual content.
For secure applications, especially where user uploads are involved, always validate based on file content β not just extensions.