MIME Type Utilities (Lite)

1.0.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `getType` and `getExtension` to determine MIME types from extensions and vice versa, including a simple validation example.

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')}`);

view raw JSON →