mime-file-extension

LIB

-

Convert MIME types to file extensions and back

mime-file-extension is a small but powerful utility written in TypeScript that helps translate between MIME types and file extensions with minimal overhead.

In modern web and backend systems, files are often identified either by their extension (.png, .json) or MIME type (image/png, application/json). This library provides an instant way to go in both directions.

Features:

  • Bidirectional mapping between file extensions and MIME types
  • Statically prebuilt map for high performance
  • Fully typed with TypeScript definitions
  • No external dependencies – zero-runtime cost
  • Robust error-tolerant fallback behavior

Use cases:

  • Building custom file upload validators
  • Setting correct Content-Type headers for HTTP responses
  • Converting file names/extensions when processing user-uploaded content
  • Generating accurate metadata for file storage systems or APIs

Example:


getMimeType('webp');       // Returns: 'image/webp'
getExtension('text/plain'); // Returns: 'txt'

getMimeType('pdf');         // 'application/pdf'
getExtension('application/xml'); // 'xml'

This library is designed to fit easily into any toolchain where correctness and speed matter. Whether you're serving files in a Node.js server, processing uploads in a cloud function, or generating documentation, this mapping utility offers predictable results every time.