mime
A comprehensive library for mime-type mapping
Last updated a year ago by broofa .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install mime 
SYNC missed versions from official npm registry.

Mime

A comprehensive, compact MIME type module.

Mime CI NPM version NPM downloads

Important mime@4 is currently in beta. To install:

npm install mime@beta

Starting with mime@4:

  • ESM module support is required. See the ESM Module FAQ. If you need CommonJS support (e.g. require('mime')), use mime@3.
  • ES2020 support is required
  • Typescript types are built-in. (@types/mime is no longer needed)

See CHANGELOG.md for further details

Install

npm install mime

Quick Start

For the full version (800+ MIME types, 1,000+ extensions):

import mime from 'mime';

mime.getType('txt');                    // ⇨ 'text/plain'
mime.getExtension('text/plain');        // ⇨ 'txt'

See Mime API below for API details.

Lite Version

The "lite" version of this module omits vendor-specific (*/vnd.*) and experimental (*/x-*) types. It weighs in at ~2.5KB, compared to 8KB for the full version. To load the lite version:

import mime from 'mime/lite';

Mime .vs. mime-types .vs. mime-db modules

For those of you wondering about the difference between these [popular] NPM modules, here's a brief rundown ...

mime-db is "the source of truth" for MIME type information. It is a dataset (JSON file), not an API, with mime type definitions pulled from a variety of authoritative sources.

mime-types is a thin wrapper around mime-db that provides an API that is mostly-compatible with mime @ < v1.3.6.

mime (this project) is like mime-types, but with the following enhancements:

  • Resolves type conflicts (See mime-score for details)
  • Smaller (mime is 2-8KB, mime-types is 18KB)
  • Zero dependencies
  • Built-in TS support

API

mime.getType(pathOrExtension)

Get mime type for the given file path or extension. E.g.

mime.getType('js');             // ⇨ 'application/javascript'
mime.getType('json');           // ⇨ 'application/json'

mime.getType('txt');            // ⇨ 'text/plain'
mime.getType('dir/text.txt');   // ⇨ 'text/plain'
mime.getType('dir\\text.txt');  // ⇨ 'text/plain'
mime.getType('.text.txt');      // ⇨ 'text/plain'
mime.getType('.txt');           // ⇨ 'text/plain'

null is returned in cases where an extension is not detected or recognized

mime.getType('foo/txt');        // ⇨ null
mime.getType('bogus_type');     // ⇨ null

mime.getExtension(type)

Get file extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.

mime.getExtension('text/plain');               // ⇨ 'txt'
mime.getExtension('application/json');         // ⇨ 'json'
mime.getExtension('text/html; charset=utf8');  // ⇨ 'html'

mime.getAllExtensions(type)

Note New in mime@4

Get all file extensions for the given mime type.

mime.getAllExtensions('image/jpeg'); // ⇨ Set(3) { 'jpeg', 'jpg', 'jpe' }

Custom Mime instances

The default objects exported by mime are immutable by design. Mutable versions can be created as follows...

new Mime(type map [, type map, ...])

Create a new, custom mime instance. For example, to create a mutable version of the default mime instance:

import { Mime } from 'mime/lite';

import standardTypes from 'mime/types/standard.js';
import otherTypes from 'mime/types/other.js';

const mime = new Mime(standardTypes, otherTypes);

Each argument is passed to the define() method, below. For example new Mime(standardTypes, otherTypes) is synonomous with new Mime().define(standardTypes).define(otherTypes)

mime.define(type map [, force = false])

Note Only available on custom Mime instances

Define MIME type -> extensions.

Attempting to map a type to an already-defined extension will throw unless the force argument is set to true.

mime.define({'text/x-abc': ['abc', 'abcd']});

mime.getType('abcd');            // ⇨ 'text/x-abc'
mime.getExtension('text/x-abc')  // ⇨ 'abc'

Command Line

Extension -> type

$ mime scripts/jquery.js
application/javascript

Type -> extension

$ npx mime -r image/jpeg
jpeg

Markdown generated from src/README_js.md by RunMD Logo

Current Tags

  • 4.0.4                                ...           latest (4 months ago)

53 Versions

  • 4.0.4                                ...           4 months ago
  • 4.0.3                                ...           7 months ago
  • 4.0.2                                ...           7 months ago
  • 4.0.1                                ...           a year ago
  • 4.0.0                                ...           a year ago
  • 4.0.0-beta.1                                ...           a year ago
  • 4.0.0-beta.0                                ...           a year ago
  • 3.0.0                                ...           3 years ago
  • 2.6.0                                ...           3 years ago
  • 2.5.2                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.7                                ...           4 years ago
  • 2.4.6                                ...           4 years ago
  • 2.4.5                                ...           5 years ago
  • 2.4.4                                ...           5 years ago
  • 2.4.3                                ...           6 years ago
  • 2.4.2                                ...           6 years ago
  • 2.4.1                                ...           6 years ago
  • 2.4.0                                ...           6 years ago
  • 2.3.1                                ...           7 years ago
  • 2.3.0                                ...           7 years ago
  • 2.2.2                                ...           7 years ago
  • 2.2.1                                ...           7 years ago
  • 2.2.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.5                                ...           7 years ago
  • 1.6.0                                ...           7 years ago
  • 1.5.0                                ...           7 years ago
  • 2.0.3                                ...           7 years ago
  • 1.4.1                                ...           7 years ago
  • 2.0.2                                ...           7 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.4.0                                ...           7 years ago
  • 1.3.6                                ...           8 years ago
  • 1.3.5 [deprecated]           ...           8 years ago
  • 1.3.4                                ...           10 years ago
  • 1.3.3                                ...           10 years ago
  • 1.3.2                                ...           10 years ago
  • 1.3.0                                ...           10 years ago
  • 1.2.11                                ...           11 years ago
  • 1.2.10                                ...           11 years ago
  • 1.2.9                                ...           12 years ago
  • 1.2.8                                ...           12 years ago
  • 1.2.7                                ...           12 years ago
  • 1.2.6                                ...           12 years ago
  • 1.2.5                                ...           13 years ago
  • 1.2.4                                ...           13 years ago
  • 1.2.3                                ...           13 years ago
  • 1.2.2                                ...           14 years ago
  • 1.2.1                                ...           14 years ago
  • 1.1.0                                ...           14 years ago
  • 1.0.0                                ...           14 years ago
Maintainers (1)
Downloads
Total 36
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE