MIME Type Lookup Utility
The `mime-lookup` library provides a focused utility for resolving MIME types based on file extensions. Unlike some alternatives, this package does not bundle its own MIME type database. Instead, it requires the user to supply a database, typically by integrating with the `mime-db` package. As of its only released version, 0.0.2, it offers core functionalities such as `lookup` for determining a MIME type from a given path, `extension` for retrieving a default file extension from a MIME type, and `define` for adding custom MIME type to extension mappings. It also includes `charsets.lookup` for charset mappings. Key differentiators include its lightweight nature due to the decoupled database, allowing for flexible custom database integration. First published in 2015 and last updated in 2016, the project appears to be unmaintained, making its release cadence non-existent.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'extensions') (or similar for 'charsets' or 'types')
cause The `MimeLookup` instance was created without providing a database, or the provided database object does not have the expected structure.fixEnsure you pass a valid MIME type database object, such as the one from `mime-db`, during instantiation: `const mime = new MimeLookup(require('mime-db'));` -
Error: Cannot find module 'mime-db'
cause The `mime-db` package, which is commonly used to provide the MIME type database, has not been installed.fixInstall `mime-db` as a dependency: `npm install mime-db`. -
Incorrect MIME type (e.g., 'application/octet-stream') returned for a known file extension.
cause The provided database is either missing the entry for the specific file extension, or the `lookup` method's input path is malformed, preventing proper extension extraction.fixVerify that your database (e.g., `mime-db` version) contains the expected entry. Also, ensure the path passed to `mime.lookup()` is correctly formatted (e.g., `'file.ext'`, `'.ext'`). You can also `mime.define()` custom types if needed.
Warnings
- breaking `mime-lookup` is an abandoned project, with its last commit in 2016 and no new releases since version 0.0.2. It is not actively maintained, which may lead to unaddressed bugs, security vulnerabilities, or compatibility issues with newer Node.js versions or ecosystems.
- gotcha The `mime-lookup` library is CommonJS-only, meaning it only supports `require()` for module imports. It does not provide ES module (ESM) support.
- gotcha `mime-lookup` does not include a MIME type database internally. You *must* provide one during instantiation, typically by using the `mime-db` package.
- gotcha The project's extremely low version number (0.0.2) from its initial release suggests it was either an early-stage project or intended to have a very minimal feature set. It may lack features, optimizations, or robust error handling found in more mature libraries.
Install
-
npm install mime-lookup -
yarn add mime-lookup -
pnpm add mime-lookup
Imports
- MimeLookup
import { MimeLookup } from 'mime-lookup';const MimeLookup = require('mime-lookup'); - mimeDb (for database)
import mimeDb from 'mime-db';
const mimeDb = require('mime-db'); - MimeLookup (instance creation)
const mime = new MimeLookup();
const mime = new MimeLookup(require('mime-db'));
Quickstart
const MimeLookup = require('mime-lookup');
const mimeDb = require('mime-db'); // This is the recommended database
// Initialize MimeLookup with the mime-db database
const mime = new MimeLookup(mimeDb);
console.log('--- MIME Type Lookups ---');
console.log('file.txt:', mime.lookup('file.txt'));
console.log('image.jpeg:', mime.lookup('image.jpeg'));
console.log('.html:', mime.lookup('.html'));
console.log('unknown.xyz (default):', mime.lookup('unknown.xyz'));
console.log('\n--- Extension Lookups ---');
console.log('text/html:', mime.extension('text/html'));
console.log('application/json:', mime.extension('application/json'));
console.log('\n--- Custom Mappings ---');
mime.define({
'application/x-custom-format': ['xcf', 'customfmt'],
'text/markdown': ['md', 'markdown']
});
console.log('document.xcf:', mime.lookup('document.xcf'));
console.log('report.md:', mime.lookup('report.md'));
console.log('application/x-custom-format (extension):', mime.extension('application/x-custom-format'));