{"id":11783,"library":"react-native-mime-types","title":"MIME Types Utility for React Native","description":"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.","status":"active","version":"2.5.0","language":"javascript","source_language":"en","source_url":"https://github.com/Liroo/react-native-mime-types","tags":["javascript","mime","types","typescript"],"install":[{"cmd":"npm install react-native-mime-types","lang":"bash","label":"npm"},{"cmd":"yarn add react-native-mime-types","lang":"bash","label":"yarn"},{"cmd":"pnpm add react-native-mime-types","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library exports a namespace object, not a default export. Access functions via `mime.lookup`, `mime.contentType`, etc.","wrong":"import mime from 'react-native-mime-types';","symbol":"mime","correct":"import * as mime from 'react-native-mime-types';"},{"note":"For CommonJS environments, the entire API is exposed as properties of the `mime` object.","wrong":"const { lookup, contentType } = require('react-native-mime-types');","symbol":"mime","correct":"const mime = require('react-native-mime-types');"},{"note":"Functions like `lookup` and `contentType` are properties of the main `mime` object and are not directly named exports.","wrong":"import { lookup } from 'react-native-mime-types';\nlookup('file.json');","symbol":"mime.lookup","correct":"import * as mime from 'react-native-mime-types';\nmime.lookup('file.json');"}],"quickstart":{"code":"import * as mime from 'react-native-mime-types';\n\nconsole.log('--- MIME Type Lookups ---');\nconsole.log('JSON file type:', mime.lookup('json')); // 'application/json'\nconsole.log('Markdown file type:', mime.lookup('.md')); // 'text/x-markdown'\nconsole.log('HTML file type:', mime.lookup('file.html')); // 'text/html'\nconsole.log('JavaScript file type:', mime.lookup('folder/file.js')); // 'application/javascript'\nconsole.log('Unrecognized file type (should be false):', mime.lookup('cats')); // false\n\nconsole.log('\\n--- Content-Type Header Creation ---');\nconsole.log('Markdown content type header:', mime.contentType('markdown')); // 'text/x-markdown; charset=utf-8'\nconsole.log('JSON content type header:', mime.contentType('file.json')); // 'application/json; charset=utf-8'\n\nconsole.log('\\n--- Extension Lookup ---');\nconsole.log('Extension for octet-stream:', mime.extension('application/octet-stream')); // 'bin'\n\nconsole.log('\\n--- Direct Maps ---');\nconsole.log('Type for .txt:', mime.types['txt']); // 'text/plain'\nconsole.log('Extensions for text/html:', mime.extensions['text/html']); // ['html', 'htm']","lang":"typescript","description":"Demonstrates common MIME type lookups by extension or path, content-type header generation, extension retrieval, and direct map access, including handling unrecognized types."},"warnings":[{"fix":"Implement custom fallback logic: `const type = mime.lookup(path) || 'application/octet-stream';`","message":"Unlike some MIME libraries (e.g., node-mime), `react-native-mime-types` returns `false` for unrecognized or invalid input, rather than providing a default fallback like 'application/octet-stream'. You must explicitly handle `false` returns.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"All functions are accessed directly from the imported `mime` object (e.g., `mime.lookup()`).","message":"The library does not expose a constructor; do not attempt to use `new mime()` or `new Mime()` patterns. It's a purely functional API.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"To add new or custom MIME types, contribute them to the `mime-db` project, or manage a separate, custom mapping within your application code.","message":"`react-native-mime-types` does not provide a `.define()` method to add custom MIME types at runtime. All type definitions are based on `mime-db`.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"This library does not allow runtime definition of MIME types. Rely on `mime-db` for definitions or manage custom types manually.","cause":"Attempting to use a `.define()` method to add custom MIME types, which this library does not support.","error":"TypeError: mime.define is not a function"},{"fix":"Ensure the library is imported as a namespace object (ESM: `import * as mime from 'react-native-mime-types';`) or the full module object (CommonJS: `const mime = require('react-native-mime-types');`). All functions are properties of the `mime` object.","cause":"The `mime` object was not imported correctly, or an attempt was made to destructure named exports that do not exist.","error":"TypeError: Cannot read properties of undefined (reading 'lookup')"}],"ecosystem":"npm"}