A comprehensive library for mime-type mapping
npx @tessl/cli install tessl/npm-mime@4.0.00
# MIME
1
2
MIME 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.
3
4
## Package Information
5
6
- **Package Name**: mime
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install mime`
10
11
## Core Imports
12
13
```typescript
14
import mime from "mime";
15
import { Mime } from "mime";
16
```
17
18
For lite version (without unofficial types):
19
20
```typescript
21
import mime from "mime/lite";
22
import { Mime } from "mime/lite";
23
```
24
25
CommonJS (not recommended, use ESM):
26
27
```javascript
28
const mime = require("mime");
29
const { Mime } = require("mime");
30
```
31
32
## Basic Usage
33
34
```typescript
35
import mime from "mime";
36
37
// Get MIME type from file path or extension
38
const type = mime.getType("script.js"); // "text/javascript"
39
const type2 = mime.getType("data.json"); // "application/json"
40
41
// Get default extension from MIME type
42
const ext = mime.getExtension("text/plain"); // "txt"
43
const ext2 = mime.getExtension("image/jpeg"); // "jpeg"
44
45
// Get all extensions for a MIME type (new in v4)
46
const extensions = mime.getAllExtensions("image/jpeg"); // Set(3) { "jpeg", "jpg", "jpe" }
47
```
48
49
## Architecture
50
51
MIME is built around several key components:
52
53
- **Immutable Default Instances**: Pre-configured, frozen instances with comprehensive MIME type databases
54
- **Custom Mutable Instances**: `Mime` class allowing custom type definitions and modifications
55
- **Type Database Variants**: Full version (all types) and lite version (standard types only)
56
- **ESM-First Design**: Modern ES module architecture with TypeScript support
57
- **Command-Line Interface**: Built-in CLI for shell scripting and automation
58
59
## Capabilities
60
61
### MIME Type Resolution
62
63
Core functionality for resolving MIME types from file paths and extensions, with robust path handling and null-safe operations.
64
65
```typescript { .api }
66
function getType(path: string): string | null;
67
```
68
69
[MIME Type Operations](./mime-operations.md)
70
71
### Custom MIME Instances
72
73
Create and configure custom MIME instances with your own type definitions, perfect for specialized applications or custom file types.
74
75
```typescript { .api }
76
class Mime {
77
constructor(...args: TypeMap[]);
78
define(typeMap: TypeMap, force?: boolean): this;
79
getType(path: string): string | null;
80
getExtension(type: string): string | null;
81
getAllExtensions(type: string): Set<string> | null;
82
}
83
84
type TypeMap = { [key: string]: string[] };
85
```
86
87
[Custom MIME Instances](./custom-instances.md)
88
89
### Command Line Interface
90
91
Shell interface for MIME type operations, supporting both type-to-extension and extension-to-type lookups.
92
93
```bash
94
mime script.js # Returns: text/javascript
95
mime -r image/jpeg # Returns: jpeg
96
```
97
98
[Command Line Interface](./cli.md)
99
100
## Type Definitions
101
102
```typescript { .api }
103
type TypeMap = { [key: string]: string[] };
104
105
interface MimeInstance {
106
getType(path: string): string | null;
107
getExtension(type: string): string | null;
108
getAllExtensions(type: string): Set<string> | null;
109
}
110
```