{"id":10885,"library":"file-type-mime","title":"MIME Type Detector","description":"file-type-mime is a JavaScript utility designed to identify the MIME type and file extension of a given file by analyzing its binary content, typically provided as an `ArrayBuffer`. It is currently at version 0.4.7, indicating a pre-1.0 release. While specific release cadence isn't detailed, pre-1.0 versions often imply that the API might still be subject to minor breaking changes between minor versions as the library matures. This library supports a range of common file types including various image formats (BMP, GIF, JPEG, PNG, HEIC, TIFF), documents (PDF, DOCX, XLSX, PPTX, ODT), and archives (ZIP, GZ, RAR), with an optional `extra` flag to detect generic `json` and `txt` types. Its key differentiators include its lightweight nature and its focus on direct buffer analysis without external dependencies, making it suitable for both Node.js environments and browser-based file upload scenarios where direct file content analysis is required.","status":"active","version":"0.4.7","language":"javascript","source_language":"en","source_url":"https://github.com/redmundas/file-type-mime","tags":["javascript","mime","file","type","content","content-type","mime-type","parse","bytes","typescript"],"install":[{"cmd":"npm install file-type-mime","lang":"bash","label":"npm"},{"cmd":"yarn add file-type-mime","lang":"bash","label":"yarn"},{"cmd":"pnpm add file-type-mime","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `parse` function is a named export. Default imports or CommonJS `require` will not work correctly.","wrong":"import parse from 'file-type-mime';\nconst parse = require('file-type-mime');","symbol":"parse","correct":"import { parse } from 'file-type-mime';"},{"note":"Use `import type` for type-only imports to prevent bundling issues or runtime errors in environments that don't strip types.","wrong":"import { Options } from 'file-type-mime';","symbol":"Options","correct":"import type { Options } from 'file-type-mime';"},{"note":"Like `Options`, `Result` is a type definition. Use `import type` for proper TypeScript usage.","wrong":"import { Result } from 'file-type-mime';","symbol":"Result","correct":"import type { Result } from 'file-type-mime';"}],"quickstart":{"code":"import { parse } from \"file-type-mime\";\nimport { readFileSync, existsSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\n\n// Create a dummy PDF file for demonstration if it doesn't exist\nconst dummyFilePath = resolve(\"./dummy.pdf\");\nif (!existsSync(dummyFilePath)) {\n  // In a real scenario, you'd have an actual file. This is just for a runnable example.\n  // For simplicity, we'll create a minimal valid PDF header.\n  // This won't be a full PDF, but enough to be detected by some tools.\n  const dummyPdfContent = Buffer.from('%PDF-1.4\\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\\n2 0 obj<</Type/Pages/Count 0>>endobj\\nxref\\n0 3\\n0000000000 65535 f\\n0000000009 00000 n\\n0000000057 00000 n\\ntrailer<</Size 3/Root 1 0 R>>startxref\\n106\\n%%EOF');\n  // For actual testing, replace with a real PDF or path to one.\n  // require('node:fs').writeFileSync(dummyFilePath, dummyPdfContent);\n  // For this runnable example, we'll just use a direct buffer.\n  console.log('No dummy.pdf found. Using a synthetic buffer for demonstration.');\n}\n\n// Simulate reading a file buffer (replace with actual file path if desired)\nconst fileBuffer = Buffer.from('%PDF-1.4\\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\\n2 0 obj<</Type/Pages/Count 0>>endobj\\nxref\\n0 3\\n0000000000 65535 f\\n0000000009 00000 n\\n0000000057 00000 n\\ntrailer<</Size 3/Root 1 0 R>>startxref\\n106\\n%%EOF'); // Example PDF header\n\nconst result = parse(fileBuffer.buffer); // Pass ArrayBuffer from Node.js Buffer\n\nif (result) {\n  console.log(`Detected MIME Type: ${result.mime}`);\n  console.log(`Detected File Extension: ${result.ext}`);\n} else {\n  console.log('Could not detect file type.');\n}\n\n// Example with `extra` option for text/plain\nconst textBuffer = Buffer.from('This is a plain text file.');\nconst textResult = parse(textBuffer.buffer, { extra: true });\nif (textResult) {\n  console.log(`\\nDetected MIME Type (with extra): ${textResult.mime}`);\n} else {\n  console.log('Could not detect text file type with extra option.');\n}","lang":"typescript","description":"This quickstart demonstrates how to use `file-type-mime` in a Node.js environment to parse the MIME type from a file's `ArrayBuffer`, including an example of using the `extra` option for generic text detection."},"warnings":[{"fix":"Always pin to exact versions (e.g., `\"file-type-mime\": \"0.4.7\"`) and review release notes carefully before upgrading minor versions.","message":"As a pre-1.0.0 package, the API of `file-type-mime` is not yet considered stable. Minor version updates (e.g., 0.4.x to 0.5.x) may introduce breaking changes without a major version increment.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"When working with Node.js `Buffer`s, access their underlying `ArrayBuffer` via `.buffer` property: `parse(nodeBuffer.buffer)`.","message":"The `parse` function exclusively expects an `ArrayBuffer` as its first argument. Passing a Node.js `Buffer` directly will result in a `TypeError` due to API mismatch, as Node.js `Buffer` is a `Uint8Array` but not directly an `ArrayBuffer` instance.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If expecting JSON or plain text files, ensure your call to `parse` includes the `extra` option: `parse(buffer, { extra: true })`.","message":"By default, the library focuses on common binary file formats. To detect generic text types like `application/json` or `text/plain`, you must explicitly pass `{ extra: true }` in the options object.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always check the return value of `parse` for `undefined` before attempting to access properties like `result.mime` or `result.ext`: `const result = parse(buffer); if (result) { /* use result */ } else { /* handle unknown type */ }`.","message":"The library returns `undefined` if the file type cannot be identified based on its internal signatures. This can happen for unsupported file types or corrupted buffers, requiring explicit handling of the `undefined` return.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use a named import for `parse`: `import { parse } from 'file-type-mime';`","cause":"Attempting to use `require()` or a default import (`import parse from 'file-type-mime'`) for the `parse` function.","error":"TypeError: parse is not a function"},{"fix":"Access the `ArrayBuffer` from the Node.js `Buffer` using `.buffer`: `parse(myNodeBuffer.buffer)`.","cause":"Passing a Node.js `Buffer` object directly to `parse` instead of its underlying `ArrayBuffer`.","error":"TypeError: The \"buffer\" argument must be an instance of ArrayBuffer or a TypedArray. Received an instance of Buffer"},{"fix":"Add a null check for the result: `const result = parse(buffer); if (result) { console.log(result.mime); }`","cause":"Not checking if the `parse` function returned `undefined` before attempting to access properties like `mime` or `ext`.","error":"Property 'mime' does not exist on type '{ ext: string; mime: string; } | undefined'."}],"ecosystem":"npm"}