mrmime
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
-
TypeError: (0 , mrmime_1.lookup) is not a function
cause Attempting to use CommonJS `require` with named imports incorrectly in an environment expecting ES modules, or vice-versa.fixIf using `require`, use `const { lookup } = require('mrmime');`. If in an ES module environment, ensure `import { lookup } from 'mrmime';` is used and the file is treated as an ES module (e.g., `.mjs` extension or `type: 'module'` in `package.json`). -
MIME type not found for '.xyz'
cause Looking up an extension that is not present in `mrmime`'s internal dictionary, which will result in `lookup` returning `undefined`.fixRecognize that `lookup` returns `undefined` for unknown extensions. If the extension is valid for your application, add it to the `mimes` dictionary: `mimes['xyz'] = 'your/custom-type';`.
Warnings
- breaking The `lookup` function's return signature changed from `string | void` to `string | undefined` in v2.0.0. This aligns with TypeScript's `undefined` type for missing values.
- breaking Upgrading to `mrmime` v2.0.0 includes an updated `mime-db` version, which resulted in changes to some extension-to-MIME-type mappings. Specifically, the `es` extension was removed, and others like `asc` had their values changed. Review the full changelog for details.
- gotcha In `mrmime` versions up to `v1.0.0`, TypeScript 4.7+ environments might not correctly discover the type definitions. This was fixed in `v1.0.1` by adding the 'types' export condition.
Install
-
npm install mrmime -
yarn add mrmime -
pnpm add mrmime
Imports
- lookup
const lookup = require('mrmime').lookup;import { lookup } from 'mrmime'; - mimes
const mimes = require('mrmime').mimes;import { mimes } from 'mrmime'; - mrmime (CommonJS destructuring)
const { lookup, mimes } = require('mrmime');
Quickstart
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}'.`);
}