MIME Database
mime-db is a comprehensive, machine-readable database of media types and associated metadata. It aggregates data from official sources such as IANA, Apache, and nginx, providing detailed information including common file extensions, compressibility, and default charsets for thousands of MIME types. The current stable version is 1.54.0. The package maintains a frequent release cadence, primarily driven by updates to its upstream data sources and community contributions for custom types. A key differentiator is its minimalist approach: it is distributed as a pure JSON data file with no accompanying logic or API, making it lightweight and un-opinionated. This design ensures maximum flexibility, although consumers must implement their own lookup logic. Crucially, changes to the underlying MIME type data are *not* considered breaking changes under its semver policy for the programmatic API, meaning data can change between minor or patch versions.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'extensions')
cause Attempting to access properties of a MIME type entry that does not exist in the database, often due to a typo in the MIME type string or querying for an unsupported type.fixAlways check if the returned entry for a MIME type is defined before attempting to access its properties, e.g., `const data = db['unknown/type']; if (data) { /* use data */ }`. -
My application's file type detection logic broke after a 'patch' or 'minor' update of mime-db.
cause This is a direct consequence of the package's semver policy where changes to the MIME type data itself are not considered breaking API changes. New extensions, removed extensions, or changes in compressibility flags can occur in non-major versions.fixPin your `mime-db` dependency to an exact version in your `package.json` (e.g., `"mime-db": "1.54.0"`) to prevent unexpected data changes. Regularly review release notes for data changes and update the pinned version proactively when desired.
Warnings
- gotcha The `mime-db` package's semver policy explicitly states that changes to the underlying MIME type data are NOT considered breaking changes for the programmatic API. This means that MIME type definitions (e.g., extensions, compressibility) can change in minor or patch releases, potentially affecting data-dependent logic without a major version bump.
- gotcha When consuming `db.json` directly from a CDN (e.g., via jsDelivr), using the `master` branch URL (`.../master/db.json`) is highly discouraged. The format or content of the JSON file on the `master` branch can change at any time, leading to unexpected issues in production.
- gotcha The database contains entries from various sources (IANA, Apache, nginx) and custom types. While efforts are made to keep it updated, there might be discrepancies or omissions, especially for less common or newly emerging media types. Relying solely on `mime-db` for critical security or validation purposes without further checks may be insufficient.
Install
-
npm install mime-db -
yarn add mime-db -
pnpm add mime-db
Imports
- db
import { db } from 'mime-db';import db from 'mime-db';
- db
const db = require('mime-db'); - direct CDN access
fetch('https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json').then(res => res.json());fetch('https://cdn.jsdelivr.net/gh/jshttp/mime-db@1.54.0/db.json').then(res => res.json());
Quickstart
import db from 'mime-db';
interface MimeEntry {
source?: 'apache' | 'iana' | 'nginx';
extensions?: string[];
compressible?: boolean;
charset?: string;
}
// Access the entire database
console.log(`Total MIME types in database: ${Object.keys(db).length}`);
// Get data for a specific MIME type
const javascriptData: MimeEntry = db['application/javascript'];
if (javascriptData) {
console.log(`
Information for application/javascript:
Source: ${javascriptData.source ?? 'custom'}
Extensions: ${javascriptData.extensions?.join(', ') ?? 'N/A'}
Compressible: ${javascriptData.compressible ? 'Yes' : 'No'}
Charset: ${javascriptData.charset ?? 'N/A'}
`);
}
// Find MIME types by extension (example logic, not built-in)
const findMimeTypeByExtension = (ext: string): string[] => {
const matchingTypes: string[] = [];
for (const mimeType in db) {
if (db[mimeType].extensions?.includes(ext.toLowerCase())) {
matchingTypes.push(mimeType);
}
}
return matchingTypes;
};
const htmlMimeTypes = findMimeTypeByExtension('html');
console.log(`MIME types associated with '.html': ${htmlMimeTypes.join(', ')}`);