MIME Type Utilities (Lite)
mime-lite provides a lightweight, standard set of MIME type mappings for JavaScript applications, suitable for both Node.js and browser environments. The current stable version is 1.0.3, with a release cadence that appears to be low but active, incorporating updates for new MIME types as needed (e.g., adding 'xls' in v1.0.3). Its key differentiator is being a 'lite' version, implying a smaller bundle size and a focused set of standard MIME type definitions compared to larger, more comprehensive libraries like the original `mime` package, from which it acknowledges copying its data. This makes it an efficient choice for projects where minimizing footprint is critical. It ships with TypeScript types, enhancing developer experience and type safety.
Common errors
-
TypeError: (0, _mime_lite.getType) is not a function
cause Incorrect CommonJS `require` syntax being used for a package primarily designed for named ESM exports, or incorrect destructuring in a mixed environment.fixUse `import { getType } from 'mime-lite';` for ESM projects. If using CommonJS, try `const { getType } = require('mime-lite');` or ensure your transpiler correctly handles interop. -
Cannot find module 'mime-lite' or its corresponding type declarations.
cause The `mime-lite` package is not installed, or TypeScript cannot locate its declaration files.fixEnsure the package is installed: `npm install mime-lite` or `yarn add mime-lite`. If types are missing, explicitly install `@types/mime-lite` if available, though `mime-lite` ships its own types.
Warnings
- gotcha This library is explicitly a 'lite' version, which means it may not contain as comprehensive a list of MIME types as the original `mime` library (from which it was copied). Developers should verify if all necessary obscure or uncommon MIME types are supported.
- gotcha `mime-lite` typically returns `undefined` or `null` for unsupported file extensions or MIME types. Applications should always handle these cases explicitly to prevent runtime errors.
- gotcha While the package ships with TypeScript types, incorrect import syntax (e.g., using CommonJS `require` directly with modern bundlers or incorrectly destructuring) can lead to 'is not a function' errors or type inference issues.
Install
-
npm install mime-lite -
yarn add mime-lite -
pnpm add mime-lite
Imports
- getType
const getType = require('mime-lite').getType;import { getType } from 'mime-lite'; - getExtension
const getExtension = require('mime-lite').getExtension;import { getExtension } from 'mime-lite'; - MimeLite
import MimeLite from 'mime-lite';
import * as MimeLite from 'mime-lite';
Quickstart
import { getType, getExtension } from 'mime-lite';
// Get MIME type from file extensions
const mimePdf = getType('document.pdf');
console.log(`PDF MIME type: ${mimePdf}`); // Expected: application/pdf
const mimeJpeg = getType('image.jpeg');
console.log(`JPEG MIME type: ${mimeJpeg}`); // Expected: image/jpeg
const mimeUnknown = getType('archive.xyz');
console.log(`Unknown extension MIME type: ${mimeUnknown}`); // Expected: undefined or null
// Get file extension from MIME types
const extHtml = getExtension('text/html');
console.log(`HTML extension: ${extHtml}`); // Expected: html
const extJson = getExtension('application/json');
console.log(`JSON extension: ${extJson}`); // Expected: json
const extUnsupported = getExtension('application/x-custom');
console.log(`Unsupported MIME extension: ${extUnsupported}`); // Expected: undefined or null
// Example: Basic file type validation
function isCommonTextFile(fileName: string): boolean {
const mime = getType(fileName);
return ['text/plain', 'text/html', 'application/json', 'text/css', 'text/javascript'].includes(mime ?? '');
}
console.log(`'report.txt' is a common text file: ${isCommonTextFile('report.txt')}`);
console.log(`'image.png' is a common text file: ${isCommonTextFile('image.png')}`);