3.0.0 • Published 10 days ago

mime v3.0.0

Weekly downloads
41,890,296
License
MIT
Repository
github
Last release
10 days ago

Mime

A comprehensive, compact MIME type module.

Mime CI NPM version NPM downloads

Important 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.
  • ES2019 support is required**.
  • Typescript types are built-in. (@types/mime is no longer needed)

Install

NPM

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

Mime API

Both require('mime') and require('mime/lite') return instances of the Mime class, documented below.

new Mime(typeMap, ... more maps)

Most users of this module will not need to create Mime instances directly. However if you would like to create custom mappings, you may do so as follows ...

// Require Mime class
import { Mime } from 'mime';

// Define mime type -> extensions map
const typeMap = {
  'text/abc': ['abc', 'alpha', 'bet'],
  'text/def': ['leppard'],
};

// Create and use Mime instance
const myMime = new Mime(typeMap);
myMime.getType('abc');            // ⇨ 'text/abc'
myMime.getExtension('text/def');  // ⇨ 'leppard'

If more than one map argument is provided, each map is define()ed (see below), in order.

mime.getType(pathOrExtension)

Get mime type for the given 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 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)

Get all extensions for the given mime type.

```javascript --run default
mime.getAllExtensions('image/jpeg'); // ⇨ [ 'jpeg', 'jpg', 'jpe' ]

mime.define(typeMap, force = false)

Define more type mappings.

typeMap is a map of type -> extensions, as documented in new Mime, above.

By default this method will throw an error if you try to map a type to an extension that is already assigned to another type. Passing true for the force argument will suppress this behavior (overriding any previous mapping).

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

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

Command Line

mime [path_or_extension]

E.g.

> mime scripts/jquery.js
application/javascript

Markdown generated from src/README_js.md by RunMD Logo

sendsuperagentecstatichttp-serverlesskarmaload-bmfont@google-cloud/storagevue-alioss-upload@eduardo_atw/whatsapp-webchinjowwchinjowvuedragdropuploadimagesroutez@buxuku/lxd-serverreact-native-bluetooth2killi8n-react-native-fast-image@invoice-simple/parse-serverpublishifyhtroutevidelangoe-puppeteer-platform-targetingwebux-fileuploadqiniu-mpvw-turn.ioextensible-node-servervisual-captcha-fastify-readytest-npm-node2@tessin/tcmtaobaoke_apiws4npmpack-zftestcafe-hammerhead-with-pr@back4app/back4appstreamotaoke_sdk_fangyougatsby-source-filesystem-frontmatterbidouamina-01@parcel-nightly-unofficial/reporter-dev-server@parcel-nightly-unofficial/optimizer-data-urlrn-send-sms@nicomee/puppeteer4leaflabs-api-proxymagno@laundry/coreabsass-sdfddabc-jdjdjjdospoon-merchant@laundry/laundrypwa-manifest-buildervw-turnsaas-plat-module-builderlicense-manager@arisageha/react-lazyload@arisageha/react-lazyload-fixyd-web-serverwebtorrent-jalischema-ohhc-stuffilitoprimrose-siteadnan-file-watcher@risyal/mail-helpersuperagent-connect-port@btpns-komodo/mail-helperreact-native-wk-viewranar-filesmind-storagenotiontheory-basic-buildali-oss-weiyieali-oss-weiyie1react-git-taskbarctk-reactgulp-css-url-extractecarx-build-toolwebtorrent-forkreact-native-template-rfbase@hijef/pushbulletinternal-dash-ml-toolsboilerplate-hapijswebfont-crusherreact-app-serverkarma-3fangwell-combogatsby-plugin-s3-html-last@cold-start/plugin-static-site@code-uprising/plugin-static-sitevi-node-telegram-bot-apijoi-libmeiroo-steedos-serverairscanairscan-examplewebpack-passive-middlewareyxapimodulebb-chatwhalefishreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@frxf/frxf@phil8795/pecuniarius-apieazlib
4.0.0-beta.1

10 days ago

4.0.0-beta.0

11 days ago

2.6.0

2 years ago

3.0.0

2 years ago

2.5.2

3 years ago

2.5.0

3 years ago

2.4.7

3 years ago

2.4.6

3 years ago

2.4.5

3 years ago

2.4.4

4 years ago

2.4.3

4 years ago

2.4.2

4 years ago

2.4.1

4 years ago

2.4.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.5

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

2.0.3

6 years ago

1.4.1

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.4.0

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

9 years ago

1.3.3

9 years ago

1.3.2

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.11

10 years ago

1.2.10

10 years ago

1.2.9

11 years ago

1.2.8

11 years ago

1.2.7

11 years ago

1.2.6

11 years ago

1.2.5

12 years ago

1.2.4

12 years ago

1.2.3

12 years ago

1.2.2

12 years ago

1.2.1

13 years ago

1.1.0

13 years ago

1.0.0

13 years ago