4.0.4 • Published 3 months ago

mime v4.0.4

Weekly downloads
41,890,296
License
MIT
Repository
github
Last release
3 months ago

Mime

NPM downloads Mime CI

An API for MIME type information.

  • All mime-db types
  • Compact and dependency-free mime's badge
  • Full TS support

!Note mime@4 is now latest. If you're upgrading from mime@3, note the following:

  • mime@4 is API-compatible with mime@3, with one two exceptions:
    • Direct imports of mime properties no longer supported
    • mime.define() cannot be called on the default mime object
  • ESM module support is required. ESM Module FAQ.
  • Requires an ES2020 or newer runtime
  • Built-in Typescript types (@types/mime no longer needed)

Installation

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'

Lite Version mime/lite's badge

mime/lite is a drop-in mime replacement, stripped of unofficial ("prs.*", "x-*", "vnd.*") types:

import mime from 'mime/lite';

API

mime.getType(pathOrExtension)

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

mime.getType('js');             // ⇨ 'text/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 mime objects are immutable. Custom, 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
text/javascript

Type -> extension

$ mime -r image/jpeg
jpeg
ecstatichttp-serversuperagent@types/mimelesskarmaload-bmfont@jimp/core@google-cloud/storage201601node1111wao@siourdas-vasilis/deploytos3@beskar-labs/gravity@bhunter179/telegramjs@monodrom/monodrom@moxa-cloud-platform/pulumi-components-library@snoculars/kv-asset-handler@june21x/semantic-release-github-no-comment@ray.js/cli@zjinh/electron-builder-encryptorab-gulpabwaabwa-gulpastro-no-undiciarchetype-librarybihua-nodered@frondjs/dev-servercomponennenttconstanta@dinert/utilseasy-select-rnvue-alioss-upload@eduardo_atw/whatsapp-webchinjowwchinjowvuedragdropuploadimages@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-builder@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-hapijs
4.0.4

3 months ago

4.0.3

5 months ago

4.0.2

5 months ago

4.0.1

10 months ago

4.0.0

10 months ago

4.0.0-beta.1

1 year ago

4.0.0-beta.0

1 year ago

2.6.0

3 years ago

3.0.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

4 years ago

2.4.4

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.1

6 years ago

2.3.0

6 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

7 years ago

1.3.5

7 years ago

1.3.4

10 years ago

1.3.3

10 years ago

1.3.2

10 years ago

1.3.1

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

13 years ago

1.2.1

14 years ago

1.1.0

14 years ago

1.0.0

14 years ago