MIME Type Names Database
mime-names is a JavaScript package that provides a database of human-readable names and common file extensions, keyed by MIME types. It is designed to complement `mime-db`, offering a more descriptive layer on top of MIME type definitions. This package does not aim to be an exhaustive list of all MIME types, focusing instead on popular and commonly used formats. Developers should anticipate that some less common MIME types might not be present and implement graceful handling for such cases. The current stable version is 1.0.0, and releases are typically made as needed to add new types or update existing data. Its primary differentiator is its direct, lookup-table structure and its specific focus on human-readable names and extensions rather than broader MIME type resolution logic.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'name')
cause Attempting to access properties like `.name` or `.extensions` on a MIME type that does not exist in the `mime-names` database without first checking if the entry exists.fixEnsure that the MIME type entry is not `undefined` before accessing its properties. `const data = db[mimeType]; if (data) { console.log(data.name); }` -
SyntaxError: The requested module 'mime-names' does not provide an export named 'SomeExport'
cause Incorrectly attempting to use named imports (e.g., `import { SomeExport } from 'mime-names'`) when the package only provides a default export (the entire database object).fixUse a default import to get the entire database object: `import db from 'mime-names';`. The package does not offer individual named exports for specific MIME types or utility functions.
Warnings
- gotcha The `mime-names` database is not comprehensive and explicitly states it 'does not try to be all encompassing'. It focuses on popular file formats. Your application code must gracefully handle cases where a MIME type might not exist in this database.
- gotcha The `db` object returned by `mime-names` should be treated as read-only. While JavaScript allows mutation of objects, modifying this imported database can lead to unexpected behavior or inconsistencies, especially if parts of your application rely on its original state.
Install
-
npm install mime-names -
yarn add mime-names -
pnpm add mime-names
Imports
- db
import { db } from 'mime-names';import db from 'mime-names';
- db (CommonJS)
const db = require('mime-names');
Quickstart
import db from 'mime-names';
const mimeType = 'application/json';
const data = db[mimeType];
if (data) {
console.log(`MIME Type: ${mimeType}`);
console.log(` Name: ${data.name}`);
console.log(` Extensions: ${data.extensions ? data.extensions.join(', ') : 'None'}`);
} else {
console.log(`No data found for MIME type: ${mimeType}`);
}
const unknownMimeType = 'application/x-custom-type';
const unknownData = db[unknownMimeType];
if (!unknownData) {
console.log(`\nAs expected, no data for ${unknownMimeType} (not all-encompassing).`);
}