mrmime

2.0.1 · active · verified Sun Apr 19

mrmime is a minimalist and high-performance utility designed to retrieve MIME types from file extensions or filenames. Currently at version 2.0.1, it offers a small footprint (2.8kB gzipped) and O(1) lookup times, making it significantly faster than alternatives like `mime` or `mime/lite` as demonstrated in its benchmarks. The library's comprehensive dictionary is generated from `mime-db`, which aggregates IANA, NGINX, and Apache datasets, ensuring accuracy for standard MIME types while intentionally omitting experimental and vendor-specific entries to maintain its lightweight nature. It supports both native ESM and CommonJS environments, including Deno, and allows for dictionary customization. The project maintains an active release cadence, primarily syncing with `mime-db` updates and addressing minor breaking changes between major versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to lookup MIME types for various inputs, handle unknown extensions, and dynamically customize the internal MIME dictionary.

import { lookup, mimes } from 'mrmime';

// Get a MIME type from an extension or filename
console.log(lookup('txt'));     // => "text/plain"
console.log(lookup('.txt'));    // => "text/plain"
console.log(lookup('a.txt'));   // => "text/plain"
console.log(lookup('index.html')); // => "text/html"

// Handle unknown extensions (returns undefined since v2.0.0)
console.log(lookup('.xyz'));    // => undefined

// Customize the MIME dictionary by adding a new type
mimes['xyz'] = 'hello/world';
console.log(lookup('xyz'));     // => "hello/world"

// Example with a common file type check
const filename = 'document.pdf';
const mimeType = lookup(filename);
if (mimeType) {
  console.log(`The MIME type for '${filename}' is: ${mimeType}`);
} else {
  console.log(`Could not determine MIME type for '${filename}'.`);
}

view raw JSON →