MIME Types Utility for React Native

2.5.0 · active · verified Sun Apr 19

react-native-mime-types is a focused utility for identifying and managing MIME (Multipurpose Internet Mail Extensions) types within React Native applications. It provides core functions like `lookup` to determine content types from file extensions or paths, `contentType` to construct full Content-Type headers, and `extension` to find the default file extension for a given MIME type. Unlike the general-purpose `node-mime` library, this package explicitly returns `false` for unknown types instead of providing naive fallbacks, requiring developers to implement their own fallback logic. It operates purely functionally, eschewing `new Mime()` constructors or a `.define()` API for adding custom types; all type definitions are sourced from the comprehensive `mime-db` project. The current stable version is 2.5.0, and it receives updates as needed, often reflecting changes in `mime-db`. Its primary differentiator is its simplicity and explicit behavior regarding unknown types, making it predictable for React Native environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates common MIME type lookups by extension or path, content-type header generation, extension retrieval, and direct map access, including handling unrecognized types.

import * as mime from 'react-native-mime-types';

console.log('--- MIME Type Lookups ---');
console.log('JSON file type:', mime.lookup('json')); // 'application/json'
console.log('Markdown file type:', mime.lookup('.md')); // 'text/x-markdown'
console.log('HTML file type:', mime.lookup('file.html')); // 'text/html'
console.log('JavaScript file type:', mime.lookup('folder/file.js')); // 'application/javascript'
console.log('Unrecognized file type (should be false):', mime.lookup('cats')); // false

console.log('\n--- Content-Type Header Creation ---');
console.log('Markdown content type header:', mime.contentType('markdown')); // 'text/x-markdown; charset=utf-8'
console.log('JSON content type header:', mime.contentType('file.json')); // 'application/json; charset=utf-8'

console.log('\n--- Extension Lookup ---');
console.log('Extension for octet-stream:', mime.extension('application/octet-stream')); // 'bin'

console.log('\n--- Direct Maps ---');
console.log('Type for .txt:', mime.types['txt']); // 'text/plain'
console.log('Extensions for text/html:', mime.extensions['text/html']); // ['html', 'htm']

view raw JSON →