How to Detect File MIME Type in Node.js: Reliable Methods & Examples

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 ExtensionMIME Type
.htmltext/html
.pngimage/png
.pdfapplication/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

Featuremime-typesfile-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


πŸ§ͺ 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.


Leave a Reply