A comprehensive library for mime-type mapping
npx @tessl/cli install tessl/npm-mime@4.0.0MIME is a comprehensive library for MIME type mapping that enables developers to determine file types based on extensions and vice versa. It offers a complete database of MIME types (800+ types, 1,000+ extensions) with both full and lite versions, supports TypeScript with built-in type definitions, and provides both programmatic API and command-line interface.
npm install mimeimport mime from "mime";
import { Mime } from "mime";For lite version (without unofficial types):
import mime from "mime/lite";
import { Mime } from "mime/lite";CommonJS (not recommended, use ESM):
const mime = require("mime");
const { Mime } = require("mime");import mime from "mime";
// Get MIME type from file path or extension
const type = mime.getType("script.js"); // "text/javascript"
const type2 = mime.getType("data.json"); // "application/json"
// Get default extension from MIME type
const ext = mime.getExtension("text/plain"); // "txt"
const ext2 = mime.getExtension("image/jpeg"); // "jpeg"
// Get all extensions for a MIME type (new in v4)
const extensions = mime.getAllExtensions("image/jpeg"); // Set(3) { "jpeg", "jpg", "jpe" }MIME is built around several key components:
Mime class allowing custom type definitions and modificationsCore functionality for resolving MIME types from file paths and extensions, with robust path handling and null-safe operations.
function getType(path: string): string | null;Create and configure custom MIME instances with your own type definitions, perfect for specialized applications or custom file types.
class Mime {
constructor(...args: TypeMap[]);
define(typeMap: TypeMap, force?: boolean): this;
getType(path: string): string | null;
getExtension(type: string): string | null;
getAllExtensions(type: string): Set<string> | null;
}
type TypeMap = { [key: string]: string[] };Shell interface for MIME type operations, supporting both type-to-extension and extension-to-type lookups.
mime script.js # Returns: text/javascript
mime -r image/jpeg # Returns: jpegtype TypeMap = { [key: string]: string[] };
interface MimeInstance {
getType(path: string): string | null;
getExtension(type: string): string | null;
getAllExtensions(type: string): Set<string> | null;
}